LeetCode Must-Solve Interview Questions & Core Concepts

LeetCode Must-Solve Interview Questions & Core Concepts
Technical Interview Prep · LeetCode

Solve fewer, learn deeper. Master patterns, not just problems.

How to use this list

Goal: internalize the patterns. For each pattern below: understand why it works, implement from scratch, then vary constraints.

  • Timebox 25–35 minutes per problem; if stuck, skim hints and re‑implement cleanly without peeking.
  • Repeat misses after 48 hours and 1 week.
  • Write a one‑page “pattern card” per concept with invariants + pitfalls.

Core concepts & patterns

1) Two Pointers / Sliding Window

Why it works: For ordered data or contiguous constraints, move left/right bounds to maintain validity (e.g., at‑most‑K distinct, sum ≤ K) in O(n).

2) Binary Search (including Answer Space)

Use when a predicate over the domain is monotonic. Often search the answer with a feasibility check.

3) Hashing & Prefix Tricks

Map elements to counts/indices; use prefix sums, parity masks, rolling signatures to compress state.

4) Greedy

Prove a local optimal choice leads to the global optimum (exchange argument, interval structure).

5) Divide & Conquer

Split into smaller subproblems and merge (e.g., mergesort, selection, closest pair).

6) Dynamic Programming

Define subproblems, transitions, base cases, and evaluation order (1D/2D, subsequences, intervals).

7) Graphs (BFS/DFS/Topo/Shortest Path)

Connectivity, cycle detection, ordering constraints, shortest paths (weighted or not).

8) Trees & BSTs

Use recursion/iteration for traversals; leverage BST invariants for O(h) operations and in‑order sequencing.

9) Heaps / Priority Queues

Maintain top‑k, rolling medians, or greedy picks with O(log n) updates.

10) Backtracking

Systematically explore state space with pruning; template: choose → recurse → undo.

Must‑Solve LeetCode Question Set (by pattern)

Sliding Window

Binary Search (+ Answer Space)

Hashing & Prefix

Greedy

Divide & Conquer

Dynamic Programming

Graphs

Trees & BSTs

Heaps / Priority Queues

Backtracking

Tip: Master these ~40–60 problems by pattern; you’ll generalize to many variants without grinding hundreds.

4‑week practice plan (3–5 problems/day)

Week 1
Sliding Window, Hashing/Prefix
Week 2
Binary Search, Greedy, Heaps
Week 3
Trees, Graphs, Recursion
Week 4
Dynamic Programming + Mixed review
  • Daily: 1 easy → 2 mediums → (opt) 1 hard/variant.
  • Review misses after 48 hours, then 1 week.
  • Write a quick “why it works” note for each solved problem.

Reusable snippets (pattern templates)

Sliding Window (at‑most‑k distinct)
def longest_substring_k_distinct(s, k):
    from collections import Counter
    left = 0
    freq = Counter()
    best = 0
    for right, ch in enumerate(s):
        freq[ch] += 1
        while len(freq) > k:
            freq[s[left]] -= 1
            if freq[s[left]] == 0:
                del freq[s[left]]
            left += 1
        best = max(best, right - left + 1)
    return best
Binary Search (answer space)
def min_speed(piles, h):
    import math
    lo, hi = 1, max(piles)
    def ok(speed):
        return sum(math.ceil(p / speed) for p in piles) <= h
    while lo < hi:
        mid = (lo + hi) // 2
        if ok(mid):
            hi = mid
        else:
            lo = mid + 1
    return lo
Prefix Sums (count subarrays equal to K)
def subarray_sum_k(nums, k):
    from collections import defaultdict
    count = defaultdict(int)
    count[0] = 1
    s = res = 0
    for x in nums:
        s += x
        res += count[s - k]
        count[s] += 1
    return res

Final interview checklist

  • Clarify constraints and edge cases; state target complexity.
  • Start with brute force → move to an optimal pattern.
  • Walk through examples and maintain invariants out loud.
  • Test extremes: empty/single/duplicates/negatives/large inputs.
  • Discuss tradeoffs (time/space, precompute vs on‑the‑fly).

Recommended reading:
Top LeetCode Questions for FAANG — by Devshree Bharatia
Blind 75 Must Do Leetcode

Post a Comment

0 Comments