This is your one-page reference for coding interviews. It covers every data structure, algorithm, and pattern from the entire DSA Tutorial series. Bookmark this page and review it before your next interview.
We show code templates in Python, Kotlin, and Go.
Data Structure Complexity
| Data Structure | Access | Search | Insert | Delete | Space |
|---|---|---|---|---|---|
| Array | O(1) | O(n) | O(n) | O(n) | O(n) |
| Linked List | O(n) | O(n) | O(1)* | O(1)* | O(n) |
| Stack | O(n) | O(n) | O(1) | O(1) | O(n) |
| Queue | O(n) | O(n) | O(1) | O(1) | O(n) |
| Hash Map | — | O(1) avg | O(1) avg | O(1) avg | O(n) |
| BST (balanced) | — | O(log n) | O(log n) | O(log n) | O(n) |
| Heap | — | O(n) | O(log n) | O(log n) | O(n) |
| Trie | — | O(m) | O(m) | O(m) | O(n*m) |
| Graph (adj list) | — | O(V+E) | O(1) | O(E) | O(V+E) |
| Union-Find | — | O(α(n)) | — | — | O(n) |
*With reference to the node. m = string length. α(n) = inverse Ackermann (effectively O(1)).
Sorting Algorithms
| Algorithm | Best | Average | Worst | Space | Stable |
|---|---|---|---|---|---|
| Bubble Sort | O(n) | O(n^2) | O(n^2) | O(1) | Yes |
| Insertion Sort | O(n) | O(n^2) | O(n^2) | O(1) | Yes |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes |
| Quick Sort | O(n log n) | O(n log n) | O(n^2) | O(log n) | No |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No |
| Counting Sort | O(n+k) | O(n+k) | O(n+k) | O(k) | Yes |
Big O Growth Rates
From fastest to slowest: O(1) < O(log n) < O(n) < O(n log n) < O(n^2) < O(2^n) < O(n!)
For n = 1,000,000:
- O(log n) = ~20
- O(n) = 1,000,000
- O(n log n) = ~20,000,000
- O(n^2) = 1,000,000,000,000 (too slow)
Code Templates
Binary Search
Python:
def binary_search(nums, target):
left, right = 0, len(nums) - 1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
Kotlin:
fun binarySearch(nums: IntArray, target: Int): Int {
var left = 0; var right = nums.size - 1
while (left <= right) {
val mid = left + (right - left) / 2
when {
nums[mid] == target -> return mid
nums[mid] < target -> left = mid + 1
else -> right = mid - 1
}
}
return -1
}
Go:
func binarySearch(nums []int, target int) int {
left, right := 0, len(nums)-1
for left <= right {
mid := left + (right-left)/2
if nums[mid] == target { return mid }
if nums[mid] < target { left = mid + 1 } else { right = mid - 1 }
}
return -1
}
Sliding Window (Variable Size)
Python:
def sliding_window(s):
window = {}
left = result = 0
for right in range(len(s)):
window[s[right]] = window.get(s[right], 0) + 1
while invalid(window):
window[s[left]] -= 1
if window[s[left]] == 0: del window[s[left]]
left += 1
result = max(result, right - left + 1)
return result
BFS Template
Python:
from collections import deque
def bfs(graph, start):
queue = deque([start])
visited = {start}
while queue:
node = queue.popleft()
for neighbor in graph[node]:
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
DFS Template
Python:
def dfs(graph, node, visited):
visited.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
dfs(graph, neighbor, visited)
Backtracking Template
Python:
def backtrack(candidates, path, result):
if is_solution(path):
result.append(path[:])
return
for candidate in candidates:
if is_valid(candidate, path):
path.append(candidate)
backtrack(candidates, path, result)
path.pop()
Union-Find Template
Python:
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
self.rank = [0] * n
def find(self, x):
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, x, y):
px, py = self.find(x), self.find(y)
if px == py: return False
if self.rank[px] < self.rank[py]: px, py = py, px
self.parent[py] = px
if self.rank[px] == self.rank[py]: self.rank[px] += 1
return True
Dijkstra Template
Python:
import heapq
def dijkstra(graph, start):
dist = {node: float("inf") for node in graph}
dist[start] = 0
heap = [(0, start)]
while heap:
d, node = heapq.heappop(heap)
if d > dist[node]: continue
for neighbor, weight in graph[node]:
new_dist = d + weight
if new_dist < dist[neighbor]:
dist[neighbor] = new_dist
heapq.heappush(heap, (new_dist, neighbor))
return dist
Topological Sort (Kahn’s Algorithm)
Python:
from collections import deque
def topological_sort(graph, in_degree, n):
queue = deque(i for i in range(n) if in_degree[i] == 0)
order = []
while queue:
node = queue.popleft()
order.append(node)
for neighbor in graph[node]:
in_degree[neighbor] -= 1
if in_degree[neighbor] == 0:
queue.append(neighbor)
return order if len(order) == n else []
Trie Template
Python:
class TrieNode:
def __init__(self):
self.children = {}
self.is_end = False
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, word):
node = self.root
for char in word:
if char not in node.children:
node.children[char] = TrieNode()
node = node.children[char]
node.is_end = True
def search(self, word):
node = self.root
for char in word:
if char not in node.children:
return False
node = node.children[char]
return node.is_end
def starts_with(self, prefix):
node = self.root
for char in prefix:
if char not in node.children:
return False
node = node.children[char]
return True
Pattern Recognition Quick Reference
| If the problem says… | Try this pattern |
|---|---|
| “Sorted array” + “find pair” | Two Pointers |
| “Longest/shortest substring” | Sliding Window |
| “Sorted” + “search” or “min of max” | Binary Search |
| “Grid” or “graph” + “shortest” | BFS |
| “All paths” or “connected components” | DFS |
| “Min/max/count ways” + “overlapping” | Dynamic Programming |
| “All combinations/permutations” | Backtracking |
| “Next greater/smaller” | Monotonic Stack |
| “K largest/smallest” | Heap |
| “Intervals” + “merge/overlap” | Sort + Merge Intervals |
| “Connected groups” or “union” | Union-Find |
| “Prefix matching” or “autocomplete” | Trie |
| “Ordering with dependencies” | Topological Sort |
| “Weighted shortest path” | Dijkstra |
DP Pattern Quick Reference
| Pattern | Key Signal | Example |
|---|---|---|
| Linear DP | Previous 1-2 elements | Climbing Stairs, House Robber |
| 0/1 Knapsack | Choose/skip items, capacity | Partition Equal Subset Sum |
| Unbounded Knapsack | Reuse items | Coin Change |
| String DP | Two strings, compare chars | Edit Distance, LCS |
| Grid DP | Move right/down | Unique Paths, Min Path Sum |
| Interval DP | Range [i,j], split at k | Burst Balloons |
| State Machine | Multiple states | Buy/Sell Stock with Cooldown |
Interview Checklist
Before coding:
- Read the problem twice
- Ask clarifying questions (edge cases, constraints, input size)
- Identify the pattern (spend 2-3 minutes)
- Explain your approach to the interviewer
- Discuss time and space complexity before coding
While coding:
- Write clean, readable code
- Use meaningful variable names
- Handle edge cases (empty input, single element, duplicates)
- Think out loud
After coding:
- Walk through a test case manually
- Check edge cases
- State the final time and space complexity
- Ask: “Would you like me to optimize this?”
Common Interview Mistakes
- Starting to code before explaining your approach
- Not asking clarifying questions
- Ignoring edge cases
- Not testing your solution
- Getting stuck silently (always communicate)
- Using the wrong data structure
- Off-by-one errors in loops and binary search
- String concatenation in a loop (use StringBuilder/join)
Essential Problems by Pattern (Top 20)
| # | Problem | Pattern | Difficulty |
|---|---|---|---|
| 1 | Two Sum | Hash Map | Easy |
| 15 | 3Sum | Two Pointers | Medium |
| 3 | Longest Substring Without Repeating | Sliding Window | Medium |
| 33 | Search in Rotated Sorted Array | Binary Search | Medium |
| 200 | Number of Islands | DFS | Medium |
| 102 | Binary Tree Level Order | BFS | Medium |
| 70 | Climbing Stairs | DP (Linear) | Easy |
| 322 | Coin Change | DP (Knapsack) | Medium |
| 78 | Subsets | Backtracking | Medium |
| 739 | Daily Temperatures | Monotonic Stack | Medium |
| 215 | Kth Largest Element | Heap | Medium |
| 56 | Merge Intervals | Intervals | Medium |
| 208 | Implement Trie | Trie | Medium |
| 207 | Course Schedule | Topological Sort | Medium |
| 684 | Redundant Connection | Union-Find | Medium |
| 743 | Network Delay Time | Dijkstra | Medium |
| 5 | Longest Palindromic Substring | Two Pointers | Medium |
| 76 | Minimum Window Substring | Sliding Window | Hard |
| 72 | Edit Distance | DP (String) | Medium |
| 62 | Unique Paths | DP (Grid) | Medium |
Series Complete
Congratulations — you have completed the entire DSA Tutorial series. You now have the knowledge to tackle coding interviews at any company. Remember: consistent practice beats last-minute cramming. Use the study plan from DSA-24, practice the patterns, and you will be ready.
Good luck with your interviews.
Full series: DSA from Zero to Interview Ready