Python Coding Interview

Sixteen patterns, sixty-one worked problems. Every problem gets a full Python solution, a line-by-line explanation, a complexity account, and the edge cases an interviewer will probe.

Most coding interview questions are not new problems. They are one of a small number of shapes wearing a new costume. Learn to see the shape and the code writes itself. This site takes the sixteen shapes that cover the large majority of questions, explains when to reach for each one and why the trick works, then works through the canonical problems in Python.

How to use this. Read the When to use and Core idea sections of a pattern first. Then close the page and try the Easy problem yourself. Come back for the solution. The value is in recognising the pattern under pressure, not in memorising the code.
Every solution is written to be spoken out loud. Type hints, guard clauses, and small named helpers, because an interviewer reads your code while you talk. Each problem also has a Say this out loud line: the one sentence that shows you understood the trick.

16 of 16 patterns written · 61 problems solved · 4 guides.

Start here — the four guides

Not patterns. The mechanics around them, and the fastest way to raise a score without learning another algorithm.

What each operation costs, the five modules that matter, and the traps that quietly turn O(n) into O(n²).

Read the constraint list as a hint. Input size maps to expected complexity, and several phrasings name a pattern outright.

Seven phases, a time budget, what to say when stuck, and a worked transcript from clarify to close.

Dry-run tables, five boundary checks, edge cases by input type, and a worked bug hunt on a real solution.

Part I — Arrays, Strings and Pointers

1Sliding Window2 Easy2 Medium

A contiguous run in an array or string, and you want the longest, shortest or best one.

2Two Pointers2 Easy2 Medium

A sorted array, and you need a pair, a triplet, or an in-place compaction.

3Fast and Slow Pointers3 Easy1 Medium

A linked list or an implicit sequence, and you suspect a cycle or need the midpoint.

Part II — Ordering and Rearranging

Time ranges or numeric ranges that overlap, and you need them merged, counted or trimmed.

5Cyclic Sort2 Easy1 Medium1 Hard

Numbers drawn from a bounded range, and you must find what is missing or duplicated in O(1) space.

Pointer surgery on a linked list with no extra memory allowed.

Part III — Trees and Graphs

Level-by-level processing, or the shortest path in an unweighted graph.

Explore every path, compute something bottom-up, or flood-fill a region.

Part IV — Search, Selection and Optimisation

The K best, worst or most frequent items, from a large or streaming dataset.

10Subsets (Backtracking)3 Medium1 Hard

Enumerate every combination, permutation or arrangement, pruning the dead ends.

11Modified Binary Search1 Easy3 Medium

Anything with a monotone yes/no boundary, sorted or rotated, in O(log n).

12Dynamic Programming1 Easy3 Medium

An optimal value or a count of ways, built from overlapping subproblems.

Part V — Aggregates, Stacks and Graphs

Range sums where a sliding window fails: negative values, exact targets, or divisibility.

14Monotonic Stack2 Medium2 Hard

For each element, the nearest larger or smaller one, and how far it reaches.

15Topological Sort2 Medium1 Hard

Ordering under dependencies, and detecting when no valid order exists.

16Union-Find3 Medium1 Hard

Connectivity on a graph whose edges keep arriving, where re-running DFS is too slow.

The 60-second decision table

What the question saysReach for
“contiguous subarray”, “substring”, “window of size k”Sliding Window
“sorted array” plus “pair / triplet / sum to target”Two Pointers
“cycle”, “middle of the list”, “repeats forever”Fast and Slow Pointers
“intervals”, “meetings”, “start and end times”Merge Intervals
“array of n numbers in range 1..n”, “missing”, “duplicate”Cyclic Sort
“reverse the list”, “in place”, “O(1) extra space”List Reversal
“level by level”, “minimum number of steps”, “nearest”BFS
“all paths”, “connected region”, “does a path exist”DFS
“top K”, “K most frequent”, “K closest”, “median of a stream”Heaps
“all subsets”, “all permutations”, “place N things without conflict”Backtracking
“sorted”, “rotated”, “first index where…”, “minimise the maximum”Binary Search
“how many ways”, “minimum cost”, “longest subsequence”Dynamic Programming
“subarray sums to exactly k”, “values may be negative”, “divisible by k”Prefix Sum
“next greater”, “span”, “histogram”, “how far until something taller”Monotonic Stack
“prerequisites”, “build order”, “can all tasks be finished”Topological Sort
“connected components”, “edges arrive one at a time”, “merge groups”Union-Find

Complexity you should be able to quote

The short list. The full table, with the traps that go with it, is in Guide 1.

OperationTimeNote
List index, appendO(1)Append is amortised; a resize is occasional.
List insert or pop at index 0O(n)Use collections.deque instead.
deque.popleft() / appendleft()O(1)The queue for BFS.
Dict or set lookup, insert, deleteO(1)Average. Worst case O(n) on adversarial hashes.
sorted() / list.sort()O(n log n)Timsort. Stable, and O(n) on already-sorted input.
heapq.heappush / heappopO(log n)Min-heap only. Negate values for a max-heap.
heapq.heapifyO(n)Cheaper than n pushes.
String concatenation in a loopO(n²)Build a list and "".join() it.
Slicing a[i:j]O(j - i)It copies. Easy to hide a quadratic loop this way.
x in list / x in setO(n) / O(1)The single most common accidental slowdown.