Vivek Kaushik
AboutBlogWorkMy Work Ethics
📘 C# DSA Cheat Sheet

📘 C# DSA Cheat Sheet

Created
Feb 15, 2026 11:57 AM
Tags
interview
preparation
DSA

1️⃣ Time & Space Complexity (Must Know)

Complexity
Meaning
O(1)
Constant
O(log n)
Logarithmic
O(n)
Linear
O(n log n)
Merge/Quick sort
O(n²)
Nested loops
O(2ⁿ)
Exponential
notion image

2️⃣ Arrays

Declaration

int[] arr = new int[5]; int[] arr2 = {1, 2, 3};

Access → O(1)

arr[0];

Traverse → O(n)

foreach (var x in arr) { }

Resize → Use List instead


3️⃣ List

Dynamic array.
var list = new List<int>(); list.Add(1); list.Remove(1);

Complexities

Operation
Time
Add (end)
O(1) amortized
Insert
O(n)
Remove
O(n)
Access by index
O(1)
Contains
O(n)

4️⃣ Dictionary<TKey, TValue>

Hash table.
var dict = new Dictionary<int, string>(); dict[1] = "One";

Complexities

Operation
Time
Insert
O(1) avg
Lookup
O(1) avg
Remove
O(1) avg
⚠ Worst case → O(n)

5️⃣ HashSet

Stores unique elements.
var set = new HashSet<int>(); set.Add(1);
Operations → O(1) average
Used for:
  • Duplicate detection
  • Fast membership checks

6️⃣ Stack (LIFO)

var stack = new Stack<int>(); stack.Push(1); stack.Pop();
All operations → O(1)
Used in:
  • DFS
  • Balanced parentheses
  • Backtracking

7️⃣ Queue (FIFO)

var queue = new Queue<int>(); queue.Enqueue(1); queue.Dequeue();
All operations → O(1)
Used in:
  • BFS
  • Scheduling

8️⃣ LinkedList

var list = new LinkedList<int>();
Operation
Time
Add/Remove (node known)
O(1)
Search
O(n)
Used when:
  • Frequent insert/delete in middle
  • Not for random access

9️⃣ Sorting

Built-in

Array.Sort(arr); list.Sort();
Uses introspective sort → O(n log n)

Custom Sort

list.Sort((a, b) => a - b);

🔟 Binary Search

Array.BinarySearch(arr, target);
Requirement:
  • Sorted array
Time → O(log n)

1️⃣1️⃣ Recursion Template

int Factorial(int n) { if (n <= 1) return 1; return n * Factorial(n - 1); }
Remember:
  • Base case
  • Recursive case
  • Stack overflow risk

1️⃣2️⃣ Two Pointers Pattern

int left = 0; int right = arr.Length - 1; while (left < right) { if (arr[left] + arr[right] == target) return true; left++; right--; }
Used for:
  • Sorted array problems
  • Palindromes

1️⃣3️⃣ Sliding Window

int left = 0; int sum = 0; for (int right = 0; right < arr.Length; right++) { sum += arr[right]; while (sum > target) { sum -= arr[left]; left++; } }
Used for:
  • Subarray problems
  • Longest substring

1️⃣4️⃣ BFS (Queue)

void BFS(Node root) { var queue = new Queue<Node>(); queue.Enqueue(root); while (queue.Count > 0) { var node = queue.Dequeue(); foreach (var child in node.Children) queue.Enqueue(child); } }

1️⃣5️⃣ DFS (Stack or Recursion)

void DFS(Node node) { if (node == null) return; foreach (var child in node.Children) DFS(child); }

1️⃣6️⃣ Common String Operations

string s = "hello"; s.Length; s.Substring(0, 2); s.Contains("he"); s.IndexOf("e");
⚠ Strings are immutable.

1️⃣7️⃣ LINQ Quick Recall

list.Where(x => x > 5); list.Select(x => x * 2); list.OrderBy(x => x); list.GroupBy(x => x); list.Any(); list.All();
⚠ LINQ = deferred execution.

1️⃣8️⃣ Common Interview Patterns

  • Two Sum → Dictionary
  • Detect duplicates → HashSet
  • Reverse linked list → pointers
  • Balanced parentheses → Stack
  • BFS shortest path → Queue
  • Binary search → sorted array
  • Sliding window → subarray sum

1️⃣9️⃣ Memory Considerations

  • List → dynamic array
  • Dictionary → hash table
  • String → immutable
  • Stack overflow in recursion
  • Avoid unnecessary allocations

2️⃣0️⃣ Quick Big-O Comparison

Structure
Lookup
Insert
Delete
Array
O(1)
O(n)
O(n)
List
O(1)
O(n)
O(n)
LinkedList
O(n)
O(1)*
O(1)*
Dictionary
O(1)
O(1)
O(1)
HashSet
O(1)
O(1)
O(1)
  • when node reference known

 
Table of Contents
1️⃣ Time & Space Complexity (Must Know)2️⃣ ArraysDeclarationAccess → O(1)Traverse → O(n)Resize → Use List instead3️⃣ ListComplexities4️⃣ Dictionary<TKey, TValue>Complexities5️⃣ HashSet6️⃣ Stack (LIFO)7️⃣ Queue (FIFO)8️⃣ LinkedList9️⃣ SortingBuilt-inCustom Sort🔟 Binary Search1️⃣1️⃣ Recursion Template1️⃣2️⃣ Two Pointers Pattern1️⃣3️⃣ Sliding Window1️⃣4️⃣ BFS (Queue)1️⃣5️⃣ DFS (Stack or Recursion)1️⃣6️⃣ Common String Operations1️⃣7️⃣ LINQ Quick Recall1️⃣8️⃣ Common Interview Patterns1️⃣9️⃣ Memory Considerations2️⃣0️⃣ Quick Big-O Comparison
Copyright 2026 Vivek Kaushik