Python Coding Interview — Cheat Sheet

Print it. Stick it up. Read it in the ten minutes before a call.

← Full study site

1 · What the question says → which pattern

“contiguous subarray”, “substring”, “window of size k”Sliding Window
“sorted” + “pair / triplet / sum to target”Two Pointers
“cycle”, “middle of the list”, “repeats forever”Fast & Slow
“intervals”, “meetings”, “start and end times”Merge Intervals
“n numbers in 1..n”, “missing”, “duplicate”Cyclic Sort
“reverse the list”, “in place”, “O(1) space”List Reversal
“level by level”, “fewest steps”, “nearest”BFS
“all paths”, “connected region”, “does a path exist”DFS
“top K”, “K most frequent”, “median of a stream”Heaps
“all subsets”, “all permutations”, “place N things”Backtracking
“rotated”, “first index where…”, “minimise the max”Binary Search
“how many ways”, “min cost”, “longest subsequence”DP
“sums to exactly k”, “values may be negative”Prefix Sum
“next greater”, “span”, “histogram”Monotonic Stack
“prerequisites”, “build order”, “can all finish”Topological Sort
“connected components”, “edges arrive one at a time”Union-Find

2 · Input size → expected complexity

n ≤ 12O(n!)permutations
n ≤ 20O(2ⁿ)subsets, bitmask
n ≤ 100O(n⁴)four loops
n ≤ 500O(n³)Floyd-Warshall
n ≤ 5,000O(n²)DP over pairs
n ≤ 10⁵O(n log n)sort, heap, binary search
n ≤ 10⁶O(n)one pass
n > 10⁸O(log n)search the answer

Budget 10⁷ operations per second in Python, not 10⁸.

3 · Constraints are hints

“O(1) extra space”two pointers, in-place, Floyd
“do not modify the input”cycle detection, binary search
“must be O(log n)”binary search, an instruction
“the array is sorted”never mentioned by accident
“values may be negative”kills the sliding window
“queries arrive one at a time”Union-Find, heap
n huge, k smallsize-k heap, O(n log k)
“lowercase letters only”26-slot array, O(1) space

4 · Python costs that bite

lst.append, lst.pop(), lst[i]O(1)
lst.pop(0), lst.insert(0, x)O(n)
x in lst  versus  x in setO(n) / O(1)
lst[i:j] — it copiesO(j−i)
s += ch in a loopO(n²)
"".join(parts)O(n)
deque.popleft, appendleftO(1)
heappush, heappop / heapifyO(log n) / O(n)
bisect_left / insortO(log n) / O(n)
sorted(), stable, TimsortO(n log n)

No max-heap: push -value. No TreeMap: say so.

5 · The script, 45 minutes

3 minClarify, then restate the problem and get a yes.
2 minWork their example by hand. Invent one edge case.
2 minState the brute force and its cost. Do not code it.
6 minName the waste, name the pattern, cost it, ask before coding.
18 minGuard clauses first. Real names. Narrate decisions, not syntax.
6 minDry-run a trace table. Diagnose out loud before fixing.
3 minTime, space, what n means, worst vs average. One follow-up.

6 · Six questions to ask, always

  1. How large can the input get?
  2. Can the values be negative? Zero?
  3. Can it be empty, or one element?
  4. Are duplicates possible?
  5. May I modify the input?
  6. If several answers are valid, does it matter which?

7 · Edge cases by input type

Arrayempty, one, two, all equal, sorted, reversed, all negative, zero
Stringempty, one char, all same, palindrome, case, spaces
Linked listNone, one node, two, target at head, at tail, a cycle
TreeNone, one node, left-only chain, perfect, duplicates
Graphno edges, self-loop, cycle, disconnected, duplicate edges
Gridempty, one cell, one row, all blocked, all open
Intervalsempty, touching ends, one nested inside another, identical
k / targetk = 0, k = 1, k = n, k > n, target above the total

8 · The five boundary checks

  1. Before the first iteration: are the seeds right?
  2. First iteration: does it read nums[i-1] at i = 0?
  3. Last iteration: does it read past the end?
  4. After the loop: a stack left holding items, a final group never flushed?
  5. Does every path advance something, so it terminates?

9 · Traps that cost offers

  • Length of [left, right] is right - left + 1.
  • Mark visited on push, not on pop, in BFS.
  • Append a copy of the trail in backtracking, and always un-choose.
  • Sort by end to choose intervals, by start to combine them.
  • Min-heap keeps the K largest. The root is what you evict.
  • left = mid loops forever. Use mid + 1.
  • Register the clone before recursing, or a cycle never ends.
  • [[0]*n]*m aliases one row. Use a comprehension.
  • Compare nodes with is, values with ==.
  • Recursion dies near 1000 frames. Know the iterative form.

10 · Say these out loud

  • “The brute force is O(n²). At n = 10⁵ that is 10¹⁰, too slow.”
  • “Both pointers only move forward, so it is O(n) amortised.”
  • “This is sorted, so the comparison proves one endpoint is in no solution.”
  • “Shall I code that?”
  • “I am sorting in place, which mutates the caller’s list.”
  • “Average O(1); an adversarial hash makes it O(n).”

Companion to the 16 patterns and 6 guides, and the 61-problem flashcard deck.