Learning path

Interview Problems

Classic coding interview problems solved step by step: brute force first, then the optimal pattern, in Python.

  1. 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 min read
  2. 2

    Contains Duplicate

    Does any value appear twice? Three solutions from slow to fast, and why a set wins.

    2 min read
  3. 3

    Valid Anagram

    Are two words made of exactly the same letters? Count letters with a dictionary or Counter.

    2 min read
  4. 4

    Group Anagrams

    Put words with the same letters into groups. The trick is choosing a good dictionary key.

    2 min read
  5. 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.

    2 min read
  6. 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).

    2 min read
  7. 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.

    2 min read
  8. 8

    Valid Palindrome

    Does a sentence read the same backwards, ignoring spaces and punctuation? Two pointers walking towards each other.

    2 min read
  9. 9

    Container With Most Water

    Pick two walls that hold the most water. Start wide and always move the shorter wall inwards.

    2 min read
  10. 10

    3Sum

    Find all unique triplets that sum to zero. Sort, fix one number, then run two pointers on the rest, skipping duplicates.

    2 min read
  11. 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.

    2 min read
  12. 12

    Best Time to Buy and Sell Stock

    Buy once, sell once, maximise profit. Track the cheapest price so far in a single pass.

    2 min read
  13. 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.

    2 min read
  14. 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.

    2 min read
  15. 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.

    2 min read
  16. 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.

    2 min read
  17. 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.

    2 min read
  18. 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.

    2 min read
  19. 19

    Koko Eating Bananas

    Binary search on the answer. Find the slowest eating speed that still finishes all bananas in time.

    2 min read
  20. 20

    Valid Parentheses

    Are the brackets balanced and correctly nested? Push opening brackets on a stack; each closing bracket must match the top.

    2 min read
  21. 21

    Min Stack

    Design a stack that returns its minimum in O(1). Store the minimum-so-far alongside every item.

    2 min read
  22. 22

    Daily Temperatures

    How many days until a warmer day? A monotonic stack of waiting days answers it in one pass.

    2 min read
  23. 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.

    2 min read
  24. 24

    Reverse Linked List

    Flip the direction of every arrow in a linked list with three pointers. Iterative and recursive versions.

    2 min read
  25. 25

    Merge Two Sorted Lists

    Zip two sorted linked lists into one sorted list. The dummy node trick removes every special case.

    2 min read
  26. 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.

    2 min read
  27. 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.

    3 min read
  28. 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.

    2 min read
  29. 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.

    2 min read
  30. 30

    Binary Tree Level Order Traversal

    Return the tree's values level by level. BFS with a queue, processing one whole level per loop.

    2 min read
  31. 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.

    2 min read
  32. 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.

    2 min read
  33. 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).

    2 min read
  34. 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.

    2 min read
  35. 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.

    2 min read
  36. 36

    Rotting Oranges

    How many minutes until every orange rots? Multi-source BFS where each level of the queue is one minute.

    2 min read
  37. 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.

    2 min read
  38. 38

    Network Delay Time

    How long until a signal reaches every computer? Dijkstra's shortest paths, then take the slowest arrival.

    2 min read
  39. 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.

    2 min read
  40. 40

    Subsets

    List every possible subset. For each item, choose "take it" or "skip it"; backtracking explores both.

    2 min read
  41. 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.

    2 min read
  42. 42

    Combination Sum

    Find every combination of numbers (reusable) that adds up to a target. Backtracking with a start index and early stopping.

    2 min read
  43. 43

    Climbing Stairs

    How many ways to climb n stairs taking 1 or 2 steps? The gentlest introduction to dynamic programming.

    2 min read
  44. 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.

    2 min read
  45. 45

    Coin Change

    The fewest coins that make an amount. Why greedy fails, and how a DP table builds every amount from smaller ones.

    2 min read
  46. 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.

    2 min read
  47. 47

    Longest Common Subsequence

    The longest sequence of letters two strings share in order. The doorway into 2D dynamic programming, with an animated table.

    2 min read
  48. 48

    Edit Distance

    The fewest insertions, deletions and replacements to turn one word into another. The algorithm behind spell-checkers and DNA comparison.

    2 min read
  49. 49

    Merge Intervals

    Combine overlapping time ranges into one. Sort by start, then extend or start a new block in a single sweep.

    2 min read
  50. 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.

    2 min read
  51. 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.

    2 min read