Quadratic Space On
Quadratic Space O(n2)
Time complexity gets all the attention, but the memory an algorithm reserves can fail you just as hard — a program that is fast but allocates too much simply crashes with OutOfMemory. Quadratic space, written O(n2), describes an algorithm whose peak extra memory grows in proportion to the square of the input size n. The mental picture is a grid: if your data has n items and you build a structure that holds one cell for every pair of items — an n-by-n table — you are using quadratic space. Line the items up along the top and down the side, and the box you fill is n rows by n columns.
Precise definition
An algorithm uses O(n2) space if there exist constants c and n0 such that for all inputs of size n ≥ n0, the additional memory it allocates is at most c·n2. Big-O is an upper bound: it caps the growth rate and ignores constant factors and lower-order terms, so 3n2 + 5n + 9 is still O(n2) — the n2 term dominates as n grows.
Two clarifications that separate strong candidates from weak ones. First, space normally means auxiliary (extra) space — memory you allocate beyond the input itself; the input array is usually not counted. Second, the defining behaviour is how the requirement scales. Because the quantity is proportional to n2, multiplying the input size by a factor k multiplies the memory by k2 — not by k. This squaring is the single fact interviewers most want you to internalise.
Worked example: counting the cells
Consider building a full pairwise distance matrix for n points — dist[i][j] holds the distance between point i and point j. You allocate an n×n array of numbers.
- n = 1000: the matrix has 1000 × 1000 = 1,000,000 cells. At 8 bytes per number that is 8 MB. Comfortable.
- n = 2000 (input doubled, k = 2): 2000 × 2000 = 4,000,000 cells — 32 MB. Doubling the input gave 4× (k2 = 22) the memory, not 2×.
- n = 10,000 (10× the original, k = 10): 10,000 × 10,000 = 100,000,000 cells — 800 MB. Ten times the input is 100× (k2 = 102) the memory.
Watch the trap: the input grew 10×, but the memory grew 100×. Never carry the input factor straight through to the memory factor — always square it. This is exactly why a matrix that fits on a laptop at n = 10k needs roughly 80 GB at n = 100k and cannot be held in RAM at all.
Pitfalls and what an interviewer probes
- Squaring the wrong factor. Say "10× input → 10× memory" and you have missed the entire point. It is 100×. Interviewers plant this to see if you reason from n2 or from a memorised phrase.
- Confusing time with space. Two nested loops that only keep a running counter are O(n2) time but O(1) space. Quadratic space requires you to actually store ~n2 things. Always ask: what persists in memory at the peak?
- Forgetting the recursion stack. Space includes the call stack. Also, an algorithm can be sneaky: string edit-distance with a full DP table is O(n2) space, but the same answer needs only O(n) space if you keep just the previous row.
- Ignoring the constant, then hitting a wall. Big-O hides the per-cell size. An n×n grid of 8-byte doubles versus 4-byte ints differs 2× in real bytes — irrelevant to the class, decisive to whether it fits.
- “Just store half the matrix.” For a symmetric table (e.g. a distance matrix where
dist[i][j] = dist[j][i]) you can keep only the upper triangle —n(n−1)/2cells. That halves the bytes but the class is unchanged:n(n−1)/2is still Θ(n2). Constant-factor savings never move you to a smaller class.
When it matters, and trade-offs vs neighbours
Quadratic space is common and often correct: dynamic-programming grids (edit distance, sequence alignment), dense graph adjacency matrices, all-pairs tables, and 2D memoisation caches. It is a deliberate time-for-space trade — a precomputed n×n lookup can turn repeated O(n) queries into O(1), which is a great deal when n is modest (hundreds to low thousands).
It sits sharply between its neighbours. O(n) space grows linearly — 10× input, 10× memory — and scales to billions of items. O(n2) grows by the square, so it is usually fine to n ≈ 104 and hopeless past n ≈ 105 (that is 1010 cells ≈ 80 GB). The next rung, O(n3), is dramatically worse still. The engineering instinct: reach for a quadratic table when n is bounded and small, but the moment inputs might grow large, look for a linear-space reformulation (streaming, rolling rows, sparse structures) before the k2 blow-up makes the machine fall over.
Key takeaways
- O(n2) space stores an amount of extra memory proportional to n2 — picture an n×n grid, one cell per pair of items.
- Scaling is the crux: multiply the input by k and memory multiplies by k2 (2×→4×, 10×→100×) — never conflate the input factor with the memory factor.
- Quadratic space means you actually retain ~n2 values; it is independent of time complexity and can often be cut to O(n) by keeping only the previous row/state.
- Practical for small, bounded n (tables, DP grids, dense graphs) as a time-for-space trade; switch to linear-space designs before n grows large.
🤖 Don't fully get this? Learn it with Claude
Stuck on Quadratic Space On? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Build the mental picture, not memorization.
I just read a lesson on **Quadratic Space On** (DSA) and want to truly understand it. Explain Quadratic Space On from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Socratic — adapts to where you're stuck.
Teach me **Quadratic Space On** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Active recall exposes what you missed.
Quiz me on **Quadratic Space On** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Intuition + hook + flashcards for long-term memory.
Help me remember **Quadratic Space On** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.