Action toolStage: Practice~180 min

Deliberate Practice on LeetCode Patterns

Build intuition one pattern at a time: think through several problems before coding any of them, check your approach, then code.

Jump to the worksheet ↓

Grinding hundreds of LeetCode problems end to end is slow, and it doesn't tell you where you're losing time. This is how I practice instead: one pattern at a time, thinking through several problems before coding any of them, and checking my approach before I write a line.

Steps 1–4 are thinking. Steps 5–7 are doing. You can practice them separately, and if getting to the optimal solution is your bottleneck, that's where your hours should go.

Quick start

  1. Skim the problem-solving approach below.
  2. Pick a pattern and practice a few problems from Hello Interview's list, which also covers the DS&A behind each pattern. For each problem do steps 1–4, timeboxed to about 10 minutes. Afterwards, read several different solutions, especially if you got stuck on any step.
  3. Once you're familiar with a pattern, do a harder problem with that tag.
  4. Once you're familiar with most patterns, do a LeetCode mock assessment.
  5. To practice coding, go back to those problems and solve them fully (steps 1–7).

What interviewers look for

Adapted from AlgoMonster's getting started guide:

  • Exceptional problem-solving. This one takes preparation. Problem-solving is drawing on challenges you know when you face one you don't (Pólya's How to Solve It is the classic on this).
  • Bug-free code. This comes from fluency with the patterns, and practice.
  • Communication and culture fit. These you can polish on short notice.

The seven steps

Problem solving (no keyboard needed)

  1. Understand the problem. Note the constraints and examples, and check your assumptions.
  2. Walk through examples and edge cases. This validates step 1, gives you test cases, and leads you to a brute force.
  3. Brute force, with its time and space complexity.
  4. Optimized: “can we do better?” Often the hardest and most important step. Which patterns come up, and how could they apply? What's the theoretical best time and space? Break the problem down (How to Solve It helps). Verify your approach on the examples from step 2.

In a real interview, get buy-in after each step, most of all step 4. The goal is to be so clear on the approach that your coding flows without interruptions, and any bugs are isolated to the coding steps.

Coding

  1. Pseudocode or comments (here's an example).
  2. Implementation. Ideally this is muscle memory, limited only by your typing speed.
  3. Testing. Run the test cases from step 2. Any bugs point to gaps in step 5 or 6.

Two ways to practice

  • Solve problems end to end, like in a mock interview.
  • Do deliberate practice on the steps you could improve most. That's what the worksheet below is for.

The fast drill

For when you're thinking: “I'm bottlenecked on understanding the problem and getting to the optimal solution.” Say you block out three hours to build intuition in pattern X, with no expectations on any one problem.

  1. Find a small set of pattern-X problems (Blind 75, NeetCode, Design Gurus…).
  2. For each problem: do steps 1–4. Validate step 4 against the LeetCode solutions and forum posts. Re-explain step 4 in your own words (it has to be yours; borrow from the Intuition and Approach sections of forum posts). Then do related problems to solidify it. The harder it was, the more related problems you do.
  3. First two hours: only steps 1–4.
  4. Last hour: steps 5–7 for those same problems. This is the muscle memory part.
  5. Last ten minutes: summarize what you learned, as a Loom or a writeup (Learning Artifacts helps).

The worksheet runs the clock for you, and you jot down the minutes each step takes. After a few sessions you'll see which step is your bottleneck. Shorter on time? Shrink the phases; the shape still works in an hour.

Prefer a spreadsheet? Use my tracker template, and track how long each step takes. Make a copy to get your own, or view the template.

When you're stuck on a problem

  • Look at it from four approaches (for example, four forum posts).
  • Explain it three ways: on a whiteboard, in a Loom, to a rubber duck.
  • Do similar problems: LeetCode's “Related problems”, the same tags, or ones listed in forum posts.

Stuck on step 4 or earlier for more than 10 minutes? Read the solution explanations, not the code, until you could teach it.

Ignore LeetCode's runtime percentiles. Focus on time and space complexity, the intuition (clear to you), and the clarity and concision of the code (clear to everyone). To save time, focus on the first four steps.

Which patterns to learn

In my experience, most algorithm problems are DFS, BFS or trees, and most object-design problems use hashmaps. Work through Grind 75's topics, in order. For more practice by topic, see wisdompeak/LeetCode.

When am I ready?

Calibrate with LeetCode assessments. They're randomized, relevant, and give you a completion-time percentile. Aim for the 90th percentile or better, consistently. Then do real mocks (with peers, Exponent, Pramp…) and get pass / no-pass feedback.

Randomized sets show you which concepts you're weaker on, set expectations for your timing, and build confidence. But perfect practice makes perfect, so get familiar with the patterns first.

Resources

Problem lists

Learning the patterns

Notes on topics

Binary search

For the intuition, see AlgoMonster's intro and speedrun. This template, from a popular LeetCode study guide, solves a lot of problems:

def binary_search(array) -> int:
    def condition(value) -> bool:
        pass

    left, right = min(search_space), max(search_space) # eg [0, n], [1, n], [0, n-1]
    while left < right:
        mid = left + (right - left) // 2
        if condition(mid):
            right = mid
        else:
            left = mid + 1
    # End condition: left >= right
    return left
Greedy vs. dynamic programming

Greedy works when locally optimal choices are guaranteed to be globally optimal, so the next step is always the right one (a good discussion).

Proofs of optimality, like “greedy stays ahead” and exchange arguments, are for your intuition. Don't expect to write a proof in the interview. Stanford's guide to greedy algorithms covers both.

Dynamic programming

Prerequisites: recursion (and its relatives: BFS / DFS, backtracking) and the similar patterns (greedy, hashmaps, heaps).

For interview prep, DP is relatively low-ROI, so make it a low priority: master the Grind 75 topics in order first. I solved several dozen DP problems in 2020, more in 2022, and had still forgotten them by 2023. That's why this approach exists.

The approach, mapped onto the seven steps

  • Understand: look for optimal substructure (the best answer for X uses the best answer for X−1, i.e. recursion) and overlapping subproblems (lots of repeated computation, so memoize).
  • Examples: make sure simpler patterns (greedy, sliding window, priority queue, hashmap) don't apply.
  • Brute force: “We have to try every subarray / combination… each try is incremental, so recursion.” Base case: the target condition is reached. Recursive step: from some state, try each choice that gets closer to the target. “But there's lots of repeated computation, so reuse it with DP.”
  • Optimize: recursion → memoized recursion → iterative DP array → space-optimized DP. To do that in under 5 minutes:
    • Master recursion and backtracking first.
    • Memoization is a small jump (@lru_cache or @functools.cache in Python).
    • For iteration, master DFS / BFS and other recursive → iterative conversions. The key is the recurrence relation.
    • For space, notice the recurrence only needs a few recent values. Lots of DP problems build that intuition.
  • Fluency: with practice, you go straight from brute force to the DP array, then space-optimize.

Worked examples

Each is a top LeetCode solution that walks the whole progression:

Any popular DP problem's top solution (Grind 75's, for example) shows the same process.

Knapsack

0-1 knapsack: take each item or don't, and account for both. Unbounded knapsack: take it 0, 1, 2… times, up to capacity. The DP array already holds that sum, so you don't recompute it. See Coin Change II. My annotated solution:

class Solution:
    def change(self, amount: int, coins: List[int]) -> int:
        """
        DP problem idea:
        - for each NEW coin we unlock, we can either use it 0 times, 1 time, ...
        - so to get the answer for the NEW coin, we sum up the above!

        So for our DP recurrence relation:
            dp[i][j] = ways to make amount j with coins[:i]
            = dp[i-1][j] + dp[i-1][j-coins[i]] + dp[i-1][j-2*coins[i]] + ...
            = dp[i-1][j] + (dp[i][j-coins[i]] if j >= coins[i] else 0)
        Since we populate our dp in a way that dp[i][j-coins[i]] is already filled
        and it's EXACTLY how to reach (j-coins[i]) given we can now use our NEW coin!

        Summing every use count directly would be O(C*A*A). Reusing dp[i][j-coins[i]]
        makes it Time: O(C*A), Space: O(C*A)
        1D DP soln: Time: O(C*A), Space: O(A)
        """
        if amount == 0:
            return 1
        dp = [[0 for _ in range(amount+1)] for __ in range(len(coins)+1)]
        dp[0][0] = 1
        for i in range(1, len(coins)+1):
            dp[i][0] = 1  # always only 1 way to form 0
            for j in range(1, amount+1):
                c = coins[i-1]
                # no-use: dp[i-1][j] = ways without using the coin at all
                # use: dp[i][j-c] = ways when using the coin ONE ADDITIONAL TIME
                # --> note we already calculated other coin uses in dp[i][j-c]
                dp[i][j] = dp[i-1][j] + (dp[i][j-c] if j>=c else 0)
        return dp[-1][-1]

    def change(self, amount: int, coins: List[int]) -> int:
        # 1-D DP version :)
        dp = [1]+[0]*amount
        for c in coins:  # for each new coin we unlock
            for j in range(c, amount+1):
                # for any amount j, we can use the new coin at least once (one additional time)
                # then we STILL need to fill {j-c}
                # -- so using the new coin gives us dp[j-c] new ways
                # note dp[j-c] /includes/ using the new coin additional times!
                dp[j] += dp[j-c]
        return dp[-1]

Other patterns worth knowing: quickselect and reservoir sampling.

Drill session

Autosaves in this browser only · nothing is sent anywhere

Phase 1 of 3

Thinking

Steps 1–4 only. No keyboard needed.

2:00:00

Phase lengths

Problems · steps 1–4

Timebox about 10 minutes per problem, and log the minutes each step took. Then check your approach against solutions and make it your own.

  1. 010 / 10 min
    1. 1Understand

      Restate the problem. Note the constraints and examples, and check your assumptions.

    2. 2Examples

      Walk through examples and edge cases. They're your test cases later.

    3. 3Brute force

      The simplest thing that works.

    4. 4Optimized

      Can we do better? Which patterns apply? What's the theoretical best? Verify it on your examples.

    related problems done · the harder this one was, the more related problems

Coding · steps 5–7

Go back to each problem: pseudocode, implement, test with your step 2 examples. Bugs point to gaps in your pseudocode or implementation, so note where they came from.

Summary

The last ten minutes: what clicked, and what to drill next.

Where your time went

Log minutes per step above and your bottleneck shows up here.

Past sessions

Finished sessions are kept here, so you can see which patterns you've drilled.

Want a coach in your corner for this?

These tools are the backbone of CoderCorgi Coaching. Let's work through them together.

Book a free call