Skip to content
Visual explainer 4h to learn 3 steps

Pattern review & spaced repetition

Map any new problem to a known pattern and keep every topic fresh with spaced review.

By Invicly Tech, , in Data Structures & Algorithms. About 11 minutes to read.

Topic 54 of 54

In short

Most problems are variations of a few dozen patterns. A cheat sheet that maps problem signals to patterns, combined with spaced repetition, keeps every topic fresh and speeds up recognition. Re-solving one representative problem per pattern is the most efficient review.

Explained simply

After many weeks of practice, you have seen hundreds of problems. The good news: most of them are built from a few dozen ideas, called patterns. Two pointers, sliding window, BFS, heap, binary search, dynamic programming and so on. If you can match a new problem to the right pattern quickly, you are most of the way there.

A pattern cheat sheet is your map. For each pattern, write the clues in a problem statement that point to it (the signals), a short code skeleton, its usual cost, and one classic problem that shows it best.

Memory fades quickly if you do not use it. Spaced repetition fights this: you review something just as you are about to forget it, and each successful review lets you wait longer before the next one. One day, then three, then a week, then three weeks.

Review does not mean rereading. It means solving again from a blank page. Picking one representative problem per pattern and re-solving it on a schedule keeps every topic fresh with little time.

Press Play on a diagram to walk through it one step at a time.

MapSignals to patterns
Problem signals
  • Sorted array or pairs
    • Two pointers
    • Binary search
  • Contiguous subarray or substring
    • Sliding window
    • Prefix sums
  • Shortest steps or levels
    • BFS
  • All combinations
    • Backtracking
  • Count ways or best value
    • Dynamic programming
  • Top k or streaming min
    • Heap
MapMore signals: graphs, stacks and searching the answer
Statement says
  • Prerequisites or task order
    • Topological sort
    • Kahn's algorithm, O(V + E)
  • Weighted shortest path
    • Dijkstra if weights are non-negative
    • Bellman-Ford if negatives
  • Groups that merge
    • Union-find
    • Near O(1) per operation
  • Next greater or smaller
    • Monotonic stack
    • O(n)
  • Smallest value that works
    • Binary search on the answer
    • Needs a monotonic check
  • Prefix or autocomplete
    • Trie
    • O(length of word) per lookup

In depth

Signals that point to a pattern

Look for words and limits in the statement. "Sorted" suggests binary search or two pointers. "Contiguous subarray" or "substring" suggests sliding window or prefix sums. "Top k", "kth largest" or "merge k lists" suggests a heap. "Next greater" or "previous smaller" suggests a monotonic stack. "Intervals" usually means sort by start first.

Graph words matter too. "Fewest steps" in an unweighted grid means BFS. Weighted with non-negative costs means Dijkstra. "Prerequisites" or "order of tasks" means topological sort. "Connected groups" with merging means union-find. "Prefix" or "autocomplete" means a trie.

Two signals are easy to miss. "Minimum largest" or "smallest value that works" often means binary search on the answer: guess a value, check if it is feasible, and narrow down. "Count the ways" or "best value with choices at each step" means dynamic programming.

  • Sorted: binary search or two pointers
  • Next greater: monotonic stack
  • Prerequisites: topological sort
  • Smallest value that works: binary search on the answer

What goes on one cheat sheet card

Keep each card short enough to read in a minute. It has five parts. Signals: the phrases from the last section. Template: the 5 to 10 line skeleton, such as the deque loop for BFS or the while left <= right loop for binary search. Cost: the time and space of the template, such as O(V + E) for BFS. Classic problem: one problem that shows the pattern clearly, which you will re-solve during review. Trap: the mistake you personally made last time, taken from your mistake log.

The trap line is the most valuable part, because it is specific to you. Examples: "marked visited when popping, not when pushing, so BFS added nodes twice" or "forgot that sliding window fails with negative numbers".

Write cards by hand or in a plain text file. The act of writing them is part of the learning.

LayersAnatomy of a pattern card
  1. 1
    Signals

    Phrases that suggest this pattern.

  2. 2
    Template

    5 to 10 lines of skeleton code.

  3. 3
    Cost

    Time and space of the template.

  4. 4
    Classic problem

    The one you re-solve in review.

  5. 5
    My trap

    The mistake you made last time.

One representative problem per pattern

Pick one problem per pattern that contains the core idea with little extra noise. Good choices from LeetCode: Two Sum II (two pointers), Longest Substring Without Repeating Characters (sliding window), Koko Eating Bananas (binary search on the answer), Daily Temperatures (monotonic stack), Kth Largest Element in an Array (heap), Merge Intervals (intervals), Number of Islands (grid DFS or BFS), Course Schedule (topological sort), Network Delay Time (Dijkstra), Redundant Connection (union-find), Implement Trie (trie), Subsets (backtracking), Coin Change (1D DP) and Longest Common Subsequence (2D DP).

In review, re-solve each from a blank file within 20 minutes, without notes. If it takes longer or fails, that pattern goes back to the short interval and gets one extra new problem that week.

How spaced repetition works

Memory of a fact decays quickly at first and then more slowly, a shape psychologists call the forgetting curve. Each time you successfully recall something, the decay slows down. Spaced repetition uses this: review an item just before you would forget it, then wait longer next time.

A simple system: start at 1 day. After each review, grade yourself. If you failed, reset the interval to 1 day. If it was hard, grow the interval a little (about 1.2 times). If it was fine, multiply it by about 2.5. The 2.5 comes from SM-2, the algorithm behind early SuperMemo and the basis of Anki's classic scheduler. So a pattern you know well goes roughly 1, 3, 7, 18, 45 days.

You can use Anki, a spreadsheet, or a small script. The tool matters less than doing the reviews on the day they are due.

CycleOne review card's life
  1. 1
    Due today

    Re-solve the classic problem.

  2. 2
    Grade

    Fail, hard or good.

  3. 3
    Set interval

    Fail: 1 day. Good: about x2.5.

  4. 4
    Wait

    Work on new problems meanwhile.

  5. Back to step 1

Mixing review into a weekly plan

Practising one topic at a time (blocked practice) feels productive, because the pattern is always the one from today's chapter. But in an interview, the hard part is deciding which pattern applies. Mixing topics (interleaved practice) trains exactly that decision, and learning research consistently finds it helps more with this kind of choice, even though it feels harder.

A sustainable week might look like this: on most days, first do whatever review cards are due, then one or two new problems from mixed topics. Once a week, do a timed set of three unseen problems. Once a week, read your mistake log and update the trap lines on your cards.

Keep a simple list of patterns with a confidence mark next to each. Any pattern marked weak gets one extra new problem per week until it is solid.

  • Due reviews first, then new problems
  • Mixed topics, not one chapter at a time
  • Weekly timed set and weekly log review

Learn it in 3 short steps

About 1h 20m each. Track them with a timer in your plan.

  1. 1
    Build a pattern-to-problem cheat sheet~1h 20m
  2. 2
    Re-solve one problem per pattern~1h 20m
  3. 3
    Schedule spaced review of weak topics~1h 20m

Key points

  • Review at growing intervals such as 1, 3, 7 and 21 days.
  • Write the signal, pattern and complexity on each card.
  • Focus extra review on patterns from your mistake log.

Key terms

Tap a card to flip it. 0/3 known

Examples

Mapping a new problem: minimum days to make m bouquets

Flowers bloom on day bloom[i]. A bouquet needs k adjacent bloomed flowers; you need m bouquets. What is the earliest day? Signal: "minimum day such that something is possible", and if day d works, every later day works too. That is binary search on the answer. Step 1: if m * k exceeds the number of flowers, return -1. Step 2: write ok(day), a linear scan counting runs. Step 3: binary search days between min(bloom) and max(bloom). Cost: O(n log(max - min)).

python
def min_days(bloom, m, k):
    if m * k > len(bloom): return -1
    def ok(day):                      # O(n) feasibility check
        made = run = 0
        for b in bloom:
            run = run + 1 if b <= day else 0
            if run == k: made += 1; run = 0
        return made >= m
    lo, hi = min(bloom), max(bloom)
    while lo < hi:                    # smallest day where ok is True
        mid = (lo + hi) // 2
        if ok(mid): hi = mid
        else: lo = mid + 1
    return lo
print(min_days([1, 10, 3, 10, 2], 3, 1))   # 3

A tiny spaced-repetition scheduler

You keep one card per pattern. After re-solving its classic problem, you grade the attempt and the function returns the next interval. Starting at 1 day, a card graded good every time goes 2, 5, 12 days (Python rounds 2.5 down to 2), while a failure sends it back to 1. Step 1: grade honestly. Step 2: store the due date. Step 3: each morning, do the due cards before anything new.

python
from datetime import date, timedelta

def next_interval(days, grade):
    if grade == "fail": return 1
    if grade == "hard": return max(1, round(days * 1.2))
    return max(2, round(days * 2.5))          # "good"

days = 1
for grade in ["good", "good", "good", "fail", "good"]:
    days = next_interval(days, grade)
    print(grade, days, date.today() + timedelta(days=days))

Quick reference

  • Sorted input: binary search or two pointers; contiguous range: sliding window or prefix sums
  • Top k or kth largest: heap of size k, O(n log k)
  • Next greater or smaller element: monotonic stack, O(n)
  • Prerequisites or ordering: topological sort; merging groups: union-find
  • Smallest value that works and a monotonic check: binary search on the answer
  • Card = signals, template, cost, classic problem, my trap
  • Review intervals: fail resets to 1 day, good multiplies by about 2.5
  • Review means re-solving from a blank file, not rereading

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1"Find the minimum ship capacity so all packages ship within D days." Which pattern fits best?

Question 2

2"For each day, how many days until a warmer temperature?" Which pattern fits?

Question 3

3In a spaced-repetition system, what happens when you fail a review?

Question 4

4"Courses have prerequisites; return an order to take them all." Which pattern fits?

Question 5

5Why mix topics in practice instead of doing one topic per week?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Name the pattern for each: (a) longest substring with at most k distinct letters, (b) number of islands in a grid, (c) kth largest in a stream, (d) all subsets of a set, (e) cheapest flight with non-negative prices, (f) can you split the array into two equal-sum halves.

  2. Exercise 2medium

    Write the template for your monotonic stack card using Daily Temperatures: return, for each day, how many days until a warmer one (0 if none).

  3. Exercise 3hard

    Write a script that reads reviews.csv (columns: problem, pattern, due as YYYY-MM-DD) and prints the cards due today or earlier, oldest first.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

How many DSA patterns are there?
Lists vary, but around 15 to 25 core patterns cover most interview problems. What matters is that your own cheat sheet covers every pattern you have studied, each with one classic problem.
Should I use Anki for coding problems?
It works well if each card asks you to re-solve a problem or write a template, not just to recognise an answer. A spreadsheet or a small script with due dates works too.
What should I do when a new problem matches no pattern I know?
Start from brute force, find the repeated work, and ask which structure removes it. Afterwards, add the problem to your cheat sheet as a new pattern or as a variation of an existing one.

Go deeper

ShareXLinkedInWhatsApp