Tabulation
Intermediate · 9 min · code in PHP, JavaScript, Java and Go
Where you will meet this in real life
Tabulation solves a problem by filling a table from the smallest cases up to the one you want, each cell built from cells already filled. There is no function calling itself and no guessing. It is how a spreadsheet naturally works.
Practical use cases
- Web app: Loan EMI schedule page. A banking site shows an amortisation table: month 1, month 2, up to month 240, with the balance after each payment. Each row is computed from the row above it, so the page simply fills the table downward. It never recomputes earlier months, and the whole table appears instantly. The customer can also change the loan amount with a slider and see the whole table redraw smoothly, because each redraw is just one fast loop.
- Web app: Running totals in a report. An admin dashboard shows daily sales with a cumulative column. Instead of re-adding all previous days for each row, each row adds today to yesterday's running total. A 365-row report needs 365 additions, not tens of thousands. The report can then be exported to Excel without any extra database queries, because every number is already computed from the row before it in a single pass.
- System: Route planners and bioinformatics. Route planning and DNA comparison tools fill big grids where each cell depends on its left and upper neighbours. The order of filling guarantees every needed value is ready. These tools all work the same way: start from the smallest known answers, fill a table in a sensible order, and read the final answer from the last cell.
The story: Mira Fills the Rangoli Table
Before Diwali, Mira's school planned a long rangoli border. The teacher, Mrs. Sharma, gave the class two kinds of sticker strips: short ones, 1 cm long, and long ones, 3 cm long. "In how many different patterns can we cover a border that is 10 cm long?" she asked.
Kabir began with the big border and tried to think backwards, but his notebook turned into a jungle of crossed-out patterns. Mira took a different road. "Let's not start at 10. Let's start at 0 and walk up." She drew a table with one box for each length: 0, 1, 2, 3, and so on.
Length 0 has one way (put nothing). Length 1: one way. Length 2: one way. Length 3: the last sticker could be a short one (copy what length 2 had) or a long one (copy what length 0 had), so two ways. Each box was built from boxes already filled to its left. No guessing, no going back.
Filling a table from the smallest question up to the one you want is called tabulation. It is the bottom-up cousin of memoization: no recursion, just a loop and a table.
How to spot this problem, and where you need it
A bigger answer depends on smaller answers, and you want to compute them in a clear order with no repeated work and no risk of the call stack running out.
Signs you need it
- The problem says "minimum", "maximum" or "number of ways", and the answer for size n can be built from answers for sizes below n.
- A recursive solution works but hits "maximum call stack size exceeded" or repeats the same calls.
- You can sketch the answer as a table with rows and columns, each cell depending on cells above or to the left.
Where to use it in a project
- Generating schedules and cumulative columns in financial and sales reports.
- Any calculation where month n depends on month n-1, such as balances, inventory or subscription renewals.
The solution
Decide what each cell of the table means. Fill the first cells, the base cases, by hand. Then loop from small to large, and for every cell compute its value from cells you have already filled. The last cell, or a cell in the last row, is your answer. Because the loop order guarantees ingredients are ready, nothing is computed twice.
Payoff: Before: a recursive version crashing or taking minutes for size 10,000. After: one simple loop finishing in milliseconds.
Explain it like I am 8
Think of climbing a ladder instead of jumping down from the roof. You stand on rung 0, then step to rung 1, then rung 2. Every new rung you reach is made from rungs you have already stood on. You never look down and never get lost.
How it works, step by step
- Make a table with one box for each small question, from 0 up to the size you need.
- Fill the base boxes you know without thinking (for example, length 0 has 1 way).
- Loop from small to big. For each box, build the answer from boxes that are already filled.
- The order matters: every box you read must already be written. That is why we go left to right.
- The answer is usually the last box (or the best of the last few).
The code in four languages
PHP
<?php
// Tabulation: fill a table from small to big. No recursion.
// Question: in how many ways can a border of length n be covered with 1-cm and 3-cm stickers?
function stickerWays(int $n): array {
$dp = array_fill(0, $n + 1, 0);
$dp[0] = 1; // base case: length 0 has one way (do nothing)
for ($i = 1; $i <= $n; $i++) {
$dp[$i] = $dp[$i - 1]; // last sticker is 1 cm
if ($i >= 3) $dp[$i] += $dp[$i - 3]; // last sticker is 3 cm
}
return $dp;
}
$table = stickerWays(10);
echo implode(" ", $table), "\n"; // 1 1 1 2 3 4 6 9 13 19 28
echo "Ways for length 10: ", $table[10], "\n"; // 28JavaScript
// Tabulation: fill a table from small to big. No recursion.
// Question: in how many ways can a border of length n be covered with 1-cm and 3-cm stickers?
function stickerWays(n) {
const dp = new Array(n + 1).fill(0);
dp[0] = 1; // base case: length 0 has one way (do nothing)
for (let i = 1; i <= n; i++) {
dp[i] = dp[i - 1]; // last sticker is 1 cm
if (i >= 3) dp[i] += dp[i - 3]; // last sticker is 3 cm
}
return dp;
}
const table = stickerWays(10);
console.log(table.join(" ")); // 1 1 1 2 3 4 6 9 13 19 28
console.log("Ways for length 10:", table[10]); // 28Java
import java.util.*;
public class Main {
// Tabulation: fill a table from small to big. No recursion.
// Question: in how many ways can a border of length n be covered with 1-cm and 3-cm stickers?
static int[] stickerWays(int n) {
int[] dp = new int[n + 1];
dp[0] = 1; // base case: length 0 has one way (do nothing)
for (int i = 1; i <= n; i++) {
dp[i] = dp[i - 1]; // last sticker is 1 cm
if (i >= 3) dp[i] += dp[i - 3]; // last sticker is 3 cm
}
return dp;
}
public static void main(String[] args) {
int[] table = stickerWays(10);
StringJoiner sj = new StringJoiner(" ");
for (int v : table) sj.add(String.valueOf(v));
System.out.println(sj); // 1 1 1 2 3 4 6 9 13 19 28
System.out.println("Ways for length 10: " + table[10]); // 28
}
}Go
package main
import "fmt"
// Tabulation: fill a table from small to big. No recursion.
// Question: in how many ways can a border of length n be covered with 1-cm and 3-cm stickers?
func stickerWays(n int) []int {
dp := make([]int, n+1)
dp[0] = 1 // base case: length 0 has one way (do nothing)
for i := 1; i <= n; i++ {
dp[i] = dp[i-1] // last sticker is 1 cm
if i >= 3 {
dp[i] += dp[i-3] // last sticker is 3 cm
}
}
return dp
}
func main() {
table := stickerWays(10)
for i, v := range table {
if i > 0 {
fmt.Print(" ")
}
fmt.Print(v)
}
fmt.Println() // 1 1 1 2 3 4 6 9 13 19 28
fmt.Println("Ways for length 10:", table[10]) // 28
}Time: O(n) · Extra space: O(n). One box per length and a tiny bit of work per box. The table can often be shrunk (see Space optimization).
Watch out for
- Filling in the wrong order, so a box reads a neighbour that is still empty. Always loop so dependencies come first.
- Forgetting the base box (
dp[0]). Everything else is built from it, so the whole table comes out as zeros. - Reading before the table starts, such as
dp[i - 3]wheniis 1 or 2. Guard it with anif.
Quick check
- Tabulation fills the table…
- From a random box
- From the biggest question down
- From the smallest question up to the one you want
- What is the main difference between tabulation and memoization?
- Tabulation uses a loop, memoization uses recursion
- Tabulation is always slower
- Memoization needs a sorted array
- In the sticker table, why does length 3 have 2 ways?
- Because 3 is a prime
- Last sticker 1 cm (copy length 2) or last sticker 3 cm (copy length 0)
- Because we skipped length 1