solulubeta
Sign in
Module 03

Recursion & Memoization

Learn to write recursion yourself, then turn it into memoized DP step by step. No memorising. Java only.

Most students memorise DP solutions and freeze when the question changes slightly. This module teaches the method instead: see what Java does on every call with an interactive visualiser, write recursion from a three-question recipe, draw the choice tree, then turn that recursion into memoization and a DP table in four mechanical steps. Every problem is derived the same way, in Java, with hidden tests.

  • Picture exactly what Java does on every recursive call, using the call stack and the recursion tree
  • Write a recursive method from scratch with the three-question recipe (promise, base case, smaller step)
  • Solve subset, combination and permutation questions with pick/skip and backtracking
  • Turn any recursion into memoization in four steps, then into a DP table when needed
  • Recognise the six DP patterns that cover most coding tests, and avoid the common Java bugs
44
lessons & problems
6
free to start
~15h
of study
01

See it, then write it

What Java really does when a method calls itself, the three-question recipe, base cases, and two ways to carry the answer.

01.01 · Lesson

Start Here: Learn Recursion the Right Way

Why recursion feels hard, the one method this module teaches, and a 5-day plan if you are short on time.

10 min Video Free
01.02 · Lesson

How Java Runs Recursion

Every call is a new copy with its own variables. The caller waits and then continues. Code before the call runs on the way down, code after it runs on the way back up.

10 min Video Free
01.03 · Lesson

The 3-Question Recipe

Write recursion without tracing: say what the method returns, find the smallest case, and trust a smaller call. Plus how to choose base cases and why you should return the answer.

11 min Video Free
01.04 · Practice set

Trace the Output

15 predict-the-output questions on recursive Java code, every answer checked by running it: the before/after order, return values, accumulators, base-case bugs and how Java passes arrays and ints.

23 min Free
01.05 · Problem

Sum From 1 to N

Return 1 + 2 + … + n. Use recursion, not a loop or the formula.

Easy
01.06 · Problem

Reverse a String

Return "s" written backwards. Use recursion, not a loop or StringBuilder.reverse().

Easy
01.07 · Problem

Sum of Digits

Return the sum of the digits of "n". Use recursion, no loops.

Easy
01.08 · Problem

Palindrome Check

Return true if "s" reads the same forwards and backwards. Compare characters exactly, so "A" and "a" are different. Use two indices and don't build new strings.

Easy
01.09 · Problem

Fast Power

Return xⁿ modulo 1,000,000,007. n can be as large as 10¹⁸, so going down one step at a time is far too slow. Halve n instead.

Medium
01.10 · Problem

Tower of Hanoi

There are three pegs: 1, 2 and 3. Peg 1 holds "n" disks, biggest at the bottom. Move them all to peg 3:

Medium
01.11 · Problem

Fibonacci Number

Fibonacci numbers start 0, 1, and each next one is the sum of the two before it: 0, 1, 1, 2, 3, 5, 8, … Return fib(n). Write it as plain recursion. It gets slow near n = 30, and chapter 3 shows why.

Easy
02

Choices and backtracking

Draw the decision tree first. Pick or skip, loops of choices, and undoing a choice to try the next one.

02.01 · Lesson

Draw the Tree: Pick or Skip

Most recursion questions are a list of choices. Draw the choice tree for a tiny input, read the state and the base case from it, then write pick-or-skip code. One tree answers four kinds of questions.

10 min Video
02.02 · Lesson

Backtracking: Choose, Explore, Undo

When a step has many options, loop over them inside the recursion: choose, explore, undo. Covers combination sum, permutations, skipping duplicates and stopping early.

11 min Video
02.03 · Problem

Subsets

Given distinct numbers, return every subset, including the empty one and the full one. Any order is fine.

Easy
02.04 · Problem

Count Subsets With Sum K

Count the subsets of positive numbers that add up to exactly "k". Subsets are picked by position, so equal numbers at different positions count separately. The empty subset has sum 0.

Medium
02.05 · Problem

Combination Sum

Return every unique combination of candidates that adds up to target. Each number can be used many times. Order doesn't matter: [2, 2, 3] and [3, 2, 2] are the same. Any order of answers is fine.

Medium
02.06 · Problem

Permutations

Return every ordering of the given distinct numbers. Any order is fine.

Medium
02.07 · Problem

Generate Parentheses

Return every valid string of "n" pairs of brackets: every "(" is closed by a later ")". Any order is fine.

Medium
02.08 · Problem

N-Queens: Count the Boards

Place n queens on an n × n chessboard so that no two attack each other: no two share a row, a column or a diagonal. Return how many ways there are.

Hard
03

From recursion to memoization

Spot repeated work, add a memo in four steps, and turn it into a table only when you need to.

04

DP patterns for coding tests

Grids, take/skip with a target, unlimited supply, two strings, and increasing subsequences.

04.01 · Lesson

Which DP Is It? A Pattern Map

Most test DP questions are one of six shapes. How to spot each one from the question, what its state is, and what its recurrence looks like.

9 min Video
04.02 · Lesson

DP on Grids

Count paths or find the cheapest path in a grid. The state is the cell, the choices are the moves, and the table fills row by row.

9 min Video
04.03 · Problem

Unique Paths With Obstacles

A robot moves only right or down, from the top-left cell to the bottom-right cell. Cells with 1 are walls. Count the paths. If the start or the end is a wall, the answer is 0.

Medium
04.04 · Lesson

Take or Skip With a Target

The most common DP in tests: each item used at most once, and a target or capacity that shrinks when you take one. State (i, rem), two choices. Covers subset sum, knapsack and equal partition.

10 min Video
04.05 · Problem

0/1 Knapsack

A bag holds at most "W" kg. Item "i" weighs wt[i] kg and is worth val[i]. Take each item at most once. Return the most value that fits.

Medium
04.06 · Problem

Partition Equal Subset Sum

Can the array be split into two groups with equal sums? Every number goes into exactly one group.

Medium
04.07 · Lesson

Unlimited Supply: Coin Change

When an item can be used again and again, taking it keeps you on the same item. One small change to take-or-skip solves fewest coins and number of ways.

10 min Video
04.08 · Problem

Coin Change: Fewest Coins

You have an unlimited number of coins of each value. Return the fewest coins that add up to exactly amount, or −1 if it can't be done.

Medium
04.09 · Problem

Coin Change: Number of Ways

With an unlimited number of coins of each value, how many different combinations make exactly amount? Order doesn't matter: 1 + 2 and 2 + 1 are the same. Amount 0 has one combination (no coins).

Medium
04.10 · Lesson

Two Strings: LCS and Edit Distance

One pointer in each string. If the characters match, move both. If not, try each allowed move and keep the best. Covers longest common subsequence and edit distance.

9 min Video
04.11 · Problem

Longest Common Subsequence

A subsequence keeps some characters in their order (you may skip any). "ace" is a subsequence of "abcde". Return the length of the longest subsequence that both strings share.

Medium
04.12 · Problem

Edit Distance

Turn word1 into word2. Inserting, deleting or replacing one character costs 1. Return the fewest operations.

Hard
04.13 · Lesson

Increasing Subsequences and Cuts

Two shapes where a choice depends on more than the index: remember your last pick (longest increasing subsequence), or loop over where the next piece ends (word break).

10 min Video
04.14 · Problem

Longest Increasing Subsequence

Return the length of the longest strictly increasing subsequence of nums. A subsequence keeps the order but may skip elements.

Medium
04.15 · Problem

Word Break

Return true if "s" can be split into words from wordDict. A word may be used many times.

Medium
05

Exam kit

A one-page playbook, a practice set on finding the state and the recurrence, and a timed mock.