CMD Guide
HomeDSAFoundations

Linear Space On

Linear Space — O(n)

Imagine you're handed a stack of n exam papers and told to grade them. If you can grade each paper and hand it straight back without keeping anything, you use almost no extra desk space no matter how tall the stack. But if the rule is "make one photocopy of every paper before returning any of them," then a stack twice as tall needs twice as many photocopies — your desk fills up in direct proportion to the input. That proportional growth is exactly what linear space, written O(n), describes: the extra memory an algorithm needs grows as a straight-line function of the input size.

Precise definition

Let n be the size of the input and let S(n) be the amount of auxiliary memory the algorithm allocates while running — the working memory beyond the input itself. We say the algorithm uses linear space when S(n) = O(n): there exist constants c > 0 and n0 such that S(n) ≤ c·n for all n ≥ n0.

Two clarifications matter for interviews:

Worked example — counting the allocations

Consider building a frequency map, then returning the elements that appear exactly once, in order. Input is an array a of n integers.

Trace it on a = [4, 7, 4, 2, 7, 9], so n = 6:

Peak auxiliary memory = map (≤ n) + result list (≤ n) = 2n cells in the worst case. Drop the constant → O(n) space. The two loops each touch every element a constant number of times, so time is O(n) too — but notice space and time are counted separately: space asks "how many cells are alive at the peak?", not "how many operations ran?"

Common pitfalls — what an interviewer probes

When it matters — trade-offs vs. neighbouring classes

Linear space is usually the comfortable default: you can afford one pass' worth of bookkeeping, and it unlocks big time savings. The canonical trade is the hash-map trick — spend O(n) space to turn an O(n2) brute-force search into O(n) time (e.g. two-sum with a seen-set). You're buying speed with memory.

Against its neighbours:

Practical scale check: O(n) space for n = 109 32-bit ints is ~4 GB — often too much. That's why streaming/one-pass O(1)-space algorithms exist for huge inputs.

Key takeaways

🤖 Don't fully get this? Learn it with Claude

Stuck on Linear 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 **Linear Space On** (DSA) and want to truly understand it. Explain Linear 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 **Linear 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 **Linear 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 **Linear 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