Print or save this for quick pattern identification during interviews!
| When you see... | Think... | Implementation |
|---|---|---|
| "Find pair that sums to X" | Hash Map | two_sum |
| "Count frequency" | Hash Map | Counter(arr) |
| "Sorted array + two elements" | Two Pointers | left/right pointers |
| "Longest substring without..." | Sliding Window | window dict + left/right |
| "Search in sorted/rotated" | Binary Search | left/right/mid |
| "All subsets/permutations" | DFS/Backtracking | recursive with path |
| "Shortest path" | BFS | queue + visited set |
| "Optimal substructure" | Dynamic Programming | dp array |
| "Connected components" | Union-Find | parent/rank arrays |
| "Top K elements" | Heap | heapq with size k |
| Complexity | Name | Examples | Max N |
|---|---|---|---|
| O(1) | Constant | Hash lookup, array access | Any |
| O(log n) | Logarithmic | Binary search | 10^9 |
| O(n) | Linear | Single loop, hash map build | 10^8 |
| O(n log n) | Linearithmic | Sorting, balanced tree ops | 10^6 |
| O(n²) | Quadratic | Nested loops, naive DP | 10^4 |
| O(2ⁿ) | Exponential | Subsets, backtracking | ~20 |
| O(n!) | Factorial | Permutations | ~10 |
seen = {}
for i, num in enumerate(arr):
if target - num in seen:
return [seen[target - num], i]
seen[num] = ileft, right = 0, len(arr) - 1
while left < right:
if arr[left] + arr[right] == target:
return [left, right]
elif arr[left] + arr[right] < target:
left += 1
else:
right -= 1left = 0
for right in range(len(s)):
# Add s[right] to window
while window_invalid():
# Remove s[left] from window
left += 1left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1def backtrack(path, choices):
if is_solution(path):
result.append(path[:])
return
for choice in choices:
# Make choice
path.append(choice)
backtrack(path, remaining_choices)
# Undo choice
path.pop()from collections import deque
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)# Bottom-up approach
dp = [0] * (n + 1)
dp[0] = base_case
for i in range(1, n + 1):
dp[i] = some_function(dp[i-1], dp[i-2])| Issue | Wrong | Right |
|---|---|---|
| Max heap | heapq.heappush(h, x) |
heapq.heappush(h, -x) |
| Integer division | a / b (float) |
a // b (int) |
| Infinity | 999999 |
float('inf') |
| Copy list | new = old |
new = old[:] or old.copy() |
| Sort descending | arr.sort() |
arr.sort(reverse=True) |
- Clarify problem (inputs, outputs, constraints)
- Ask about edge cases
- Identify pattern
- Discuss brute force
- Optimize approach
- State time/space complexity
- Write clean, readable code
- Use meaningful variable names
- Think out loud
- Handle edge cases
- Test with example
- Walk through code
- Test edge cases
- Verify complexity
- Discuss optimizations
-
❌ Not asking clarifying questions
- Array sorted? Duplicates allowed? Empty input?
-
❌ Jumping to code too quickly
- Always discuss approach first
-
❌ Not considering edge cases
- Empty input, single element, all same values
-
❌ Poor variable naming
x,temp,datavsleft_pointer,max_sum,frequency_map
-
❌ Not explaining thought process
- Interviewer can't help if they don't know what you're thinking
-
❌ Giving up on optimization
- Brute force → Better solution (always try to optimize)
💡 If stuck, try these approaches:
- Draw examples on paper
- Start with brute force
- Look for patterns/repetition
- Consider sorting first
- Use a hash map for O(1) lookups
- Break into smaller subproblems
💡 Communication matters:
- Explain what you're doing
- Verbalize your thought process
- Ask for hints if truly stuck
- Stay calm and confident
# Reverse string
s[::-1]
# Count occurrences
from collections import Counter; Counter(arr)
# Default dict
from collections import defaultdict; d = defaultdict(list)
# Sort by custom key
arr.sort(key=lambda x: x[1])
# Get min/max
min(arr), max(arr)
# Check if all/any
all(condition for x in arr)
any(condition for x in arr)
# List comprehension with condition
[x for x in arr if condition]
# Enumerate for index + value
for i, val in enumerate(arr):
# Zip multiple iterables
for a, b in zip(arr1, arr2):
# Create 2D array
[[0]*cols for _ in range(rows)]Keep this handy during practice and interviews! 📌