Guide

Interview problems cluster. Pattern fluency means you hear "longest substring without repeating characters" and reach for a sliding window with a last-seen index map. Memorizing solutions fails when the problem is twisted 15 degrees.

Concept

A pattern trigger is the shape of the input and the question that points at a template. Keep a cheat sheet of triggers, not a repo of solutions you cannot regenerate. The problem-solving process is how you apply the template.

The big idea

You do not need every hard DP variant. You need fluent mediums in the first eight rows below and a working acquaintance with the rest.

PatternTriggerTypical complexity
Two pointersSorted array, pairs, palindromes, partitionOften O(n)
Sliding windowSubstring/subarray with a constraintO(n) with a map/set
HashingFrequency, complement lookup, anagramsExpected O(n)
Binary searchSorted search space, "minimize max"O(log n) on the space
BFS / DFSGraphs, grids, trees, unweighted shortest pathO(V+E)
BacktrackingCombinations, permutations, constraintsExponential; prune hard
HeapTop-K, merge K lists, running medianOften O(n log k)
IntervalsMerge, insert, meeting roomsSort then sweep O(n log n)
Monotonic stackNext greater, histogram rectangleO(n)
Union-findConnectivity, dynamic mergesNearly O(n) with compression
Topological sortDependencies, build orderO(V+E)
Dynamic programmingOptimal substructure + overlapping subproblemsOften O(n)–O(n²)

Drill recognition with spaced repetition: revisit tough problems after 1 day, 3 days and 1 week.

Walking through a window

Many substring problems share one template: expand the right edge, shrink the left until the window is valid, track a best answer.

Example

Longest substring without repeating characters: "abcabcbb" → 3 ("abc").

Keep left, a map of char → last index, and best. For each right, if s[right] was seen at index ≥ left, move left to that index + 1. Update last index and best = max(best, right - left + 1). Each index moves at most once → O(n) time, O(min(n, alphabet)) space.

Python
def length_of_longest_substring(s: str) -> int:
    last: dict[str, int] = {}
    left = 0
    best = 0
    for right, ch in enumerate(s):
        if ch in last and last[ch] >= left:
            left = last[ch] + 1
        last[ch] = right
        best = max(best, right - left + 1)
    return best

Output for "abcabcbb":

3

Say the invariant out loud: "window [left, right] always has unique characters." Invariants are how interviewers see that you are not guessing.

For grids and graphs, default to BFS for shortest path in unweighted graphs and DFS/backtracking for all paths or components. Clarify: directed vs undirected, weighted vs not, can you revisit nodes? A BFS skeleton (queue + visited) should be writable in under three minutes. For DP, force the questions: what is the state, the transition, the base case? Can you reduce space? If you cannot answer state and transition in two sentences, stay on the recursive definition longer.

Common mistakes

  • Code dumps instead of triggers. You cannot regenerate the solution when the prompt twists.
  • Skipping the invariant. The window "works" until an off-by-one you never stated.
  • Coding DP before the state is a sentence. You will debug a fog.

Check yourself

  1. You hear "longest substring with at most K distinct characters." Which pattern?

Sliding window, with a map of counts, shrinking left when the distinct count exceeds K.

  1. Why is a sliding window often O(n)?

Each index enters and leaves the window at most once.

Key takeaways

  • Learn triggers, not memorized listings.
  • Fluent mediums in windows, two pointers, hashing, graphs, heaps and intervals beat shallow hards.
  • State the invariant (or DP state and transition) before you type.

Further learning

NeetCode roadmapPractice coding interviews with structured courses, AI-assisted assessments, head-to-head Versus mode, and 1,000+ problems.NeetCode

Next: communicating while you code.