CMD Guide
HomeDSAFoundations

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 k2not 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.

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

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

🤖 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.

🎨 Explain it visually

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.
🤔 Walk me through it (interactive)

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.
🧪 Quiz me & fix my gaps

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.
🧠 Make it stick

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.

📝 My notes