LeetCode Problems

Interview problems, but visible.

Two Sum, sliding window, BFS on a grid — the problems that show up in every interview, with the data structure visible at every step. Pause, rewind, edit the input, and ask 'why does this work?' until you actually know.

Start here

Recommended path

New to this section? Work through these in order — each builds on the previous.

  1. 1Two Sum
  2. 2Valid Parentheses
  3. 3Reverse Linked List
  4. 4Climbing Stairs
  5. 5Binary Tree Inorder Traversal

Easy classics

6

Two Sum

Easy

The brute-force solution checks every pair in O(n²). The hash-map trick: as we scan left-to-right, for each nums[i] we look up its complement (target − nums[i]) in a map of values we've already seen. The map turns the inner loop's linear scan into an O(1) lookup, so the whole thing runs in a single O(n) pass.

hash-map lookup
O(n) · O(n)Open visualization

Valid Parentheses

Easy

Walk left-to-right with a stack. Every opener gets pushed; every closer pops and must match the top. If you ever pop the wrong opener — or pop from an empty stack — the string is invalid. After the loop, the stack must be empty for the string to be valid. The stack is the perfect data structure here because brackets nest like function calls.

stack match
O(n) · O(n)Open visualization

Best Time to Buy and Sell Stock

Easy

The brute force is to try every (buy, sell) pair — O(n²). The trick is realizing you don't need to revisit history: for each day, the best sell-today profit is today's price minus the cheapest price seen so far. So track running min and running max-profit in one pass.

track minsingle pass
O(n) · O(1)Open visualization

Climbing Stairs

Easy

To reach step n, the LAST move you made was either a 1-step (from n-1) or a 2-step (from n-2). So the number of ways to reach n is the sum of the ways to reach n-1 and n-2. That's Fibonacci. Building the dp table bottom-up — dp[0]=1, dp[1]=1, then dp[i] = dp[i-1] + dp[i-2] — gives O(n) time, O(n) space; you can compress to two variables for O(1) space.

DP base + step
O(n) · O(n) (or O(1) with two rolling variables)Open visualization

Reverse Linked List

Easy

Three pointers do the whole job: prev (everything we've reversed so far), curr (the node we're about to flip), and nxt (saved so we don't lose the rest of the list). On each iteration: save nxt, flip curr.next to point at prev, then march all three pointers one step forward. At the end, prev is the new head.

pointer reversal
O(n) · O(1)Open visualization

Merge Two Sorted Lists

Easy

Two pointers, one for each list. Each step: compare heads, take the smaller one, advance that pointer. When one list runs out, append the rest of the other. The two lists must be pre-sorted; if they aren't, you're doing merge-sort's merge step on unsorted data and you'll get garbage.

two-pointer merge
O(n + m) · O(n + m) for the outputOpen visualization

Arrays & Strings

2

Trees & Grids

2