Learning path
Interview Problems
Classic coding interview problems solved step by step: brute force first, then the optimal pattern, in Python.
-
1
Two Sum
Find two numbers that add up to a target. The most famous interview warm-up, solved in one pass with a hash map.
-
2
Contains Duplicate
Does any value appear twice? Three solutions from slow to fast, and why a set wins.
-
3
Valid Anagram
Are two words made of exactly the same letters? Count letters with a dictionary or Counter.
-
4
Group Anagrams
Put words with the same letters into groups. The trick is choosing a good dictionary key.
-
5
Top K Frequent Elements
Return the k most common numbers. Count with a dictionary, then pick the top k with a heap or bucket sort.
-
6
Product of Array Except Self
For every position, multiply everything except that number, without using division. Prefix and suffix products make it O(n).
-
7
Longest Consecutive Sequence
Find the longest run of consecutive numbers in an unsorted list in O(n), by only counting from the start of each run.
-
8
Valid Palindrome
Does a sentence read the same backwards, ignoring spaces and punctuation? Two pointers walking towards each other.
-
9
Container With Most Water
Pick two walls that hold the most water. Start wide and always move the shorter wall inwards.
-
10
3Sum
Find all unique triplets that sum to zero. Sort, fix one number, then run two pointers on the rest, skipping duplicates.
-
11
Trapping Rain Water
How much rain collects between bars of different heights? The water above each bar depends on the tallest walls on its left and right.
-
12
Best Time to Buy and Sell Stock
Buy once, sell once, maximise profit. Track the cheapest price so far in a single pass.
-
13
Longest Substring Without Repeating Characters
The textbook sliding window problem. Grow the window to the right; when a letter repeats, shrink from the left.
-
14
Minimum Window Substring
Find the smallest part of a string that contains every letter of another string. Expand until valid, then shrink while valid.
-
15
Subarray Sum Equals K
Count continuous subarrays that add up to k, even with negative numbers. Prefix sums plus a hash map of counts.
-
16
Maximum Subarray
Find the continuous part of a list with the biggest sum. Kadane's algorithm: keep going while the running sum helps, restart when it hurts.
-
17
Binary Search
Find a target in a sorted list in O(log n). The bug-free template, first and last position, and search insert position.
-
18
Search in Rotated Sorted Array
A sorted list was rotated at an unknown point. Binary search still works, because one half is always sorted.
-
19
Koko Eating Bananas
Binary search on the answer. Find the slowest eating speed that still finishes all bananas in time.
-
20
Valid Parentheses
Are the brackets balanced and correctly nested? Push opening brackets on a stack; each closing bracket must match the top.
-
21
Min Stack
Design a stack that returns its minimum in O(1). Store the minimum-so-far alongside every item.
-
22
Daily Temperatures
How many days until a warmer day? A monotonic stack of waiting days answers it in one pass.
-
23
Largest Rectangle in Histogram
The biggest rectangle that fits under a bar chart. For each bar, find how far it can stretch left and right with a monotonic stack.
-
24
Reverse Linked List
Flip the direction of every arrow in a linked list with three pointers. Iterative and recursive versions.
-
25
Merge Two Sorted Lists
Zip two sorted linked lists into one sorted list. The dummy node trick removes every special case.
-
26
Linked List Cycle
Does a linked list loop back on itself? A tortoise and a hare running at different speeds will meet if there's a loop.
-
27
LRU Cache
Design a cache that throws out the least recently used item when full, with O(1) get and put. A hash map plus an ordered list.
-
28
Maximum Depth of Binary Tree
How many levels does a tree have? A three-line recursive answer, plus the level-by-level BFS version.
-
29
Invert Binary Tree
Mirror a binary tree by swapping every node's left and right children. Short, famous and a perfect recursion warm-up.
-
30
Binary Tree Level Order Traversal
Return the tree's values level by level. BFS with a queue, processing one whole level per loop.
-
31
Validate Binary Search Tree
Is a tree a valid BST? Checking only parent and child is a trap; pass an allowed range down the tree instead.
-
32
Lowest Common Ancestor of a Binary Tree
Find the deepest node that has both p and q below it. One elegant recursion that reports what it found upwards.
-
33
Kth Largest Element in an Array
Find the k-th largest number without sorting everything. A min-heap of size k, plus quickselect for an average O(n).
-
34
Merge k Sorted Lists
Merge many sorted linked lists into one. A min-heap always tells you which list has the next smallest value.
-
35
Number of Islands
Count groups of connected land in a grid. Each time you step on new land, count it and flood fill the whole island.
-
36
Rotting Oranges
How many minutes until every orange rots? Multi-source BFS where each level of the queue is one minute.
-
37
Course Schedule
Can you finish every course given the prerequisites? Only if there's no cycle. Kahn's algorithm takes courses as soon as they're unlocked.
-
38
Network Delay Time
How long until a signal reaches every computer? Dijkstra's shortest paths, then take the slowest arrival.
-
39
Word Ladder
Change one letter at a time to turn one word into another. Words are nodes, one-letter changes are edges, and BFS finds the shortest ladder.
-
40
Subsets
List every possible subset. For each item, choose "take it" or "skip it"; backtracking explores both.
-
41
Permutations
List every ordering of a set of numbers. Fill positions one by one with numbers not used yet, then undo and try the next.
-
42
Combination Sum
Find every combination of numbers (reusable) that adds up to a target. Backtracking with a start index and early stopping.
-
43
Climbing Stairs
How many ways to climb n stairs taking 1 or 2 steps? The gentlest introduction to dynamic programming.
-
44
House Robber
Take the most money without robbing two neighbouring houses. At each house, choose "rob it" or "skip it", and remember the best so far.
-
45
Coin Change
The fewest coins that make an amount. Why greedy fails, and how a DP table builds every amount from smaller ones.
-
46
Longest Increasing Subsequence
The longest rising sequence you can pick from a list (gaps allowed). An O(n²) DP, then a clever O(n log n) "patience sorting" version.
-
47
Longest Common Subsequence
The longest sequence of letters two strings share in order. The doorway into 2D dynamic programming, with an animated table.
-
48
Edit Distance
The fewest insertions, deletions and replacements to turn one word into another. The algorithm behind spell-checkers and DNA comparison.
-
49
Merge Intervals
Combine overlapping time ranges into one. Sort by start, then extend or start a new block in a single sweep.
-
50
Meeting Rooms II
How many meeting rooms do you need? Sort by start and keep a min-heap of end times, or sweep through start and end events.
-
51
Jump Game
Can you reach the last index if each number is your maximum jump? Track the farthest reachable point in one greedy pass.