skip to content

questions

4

What does auxiliary space measure that total space complexity does not?

level: juniorimportance: must knowfreq 82%

answer

  1. one measure ignores what you were handed
  2. which one does an interviewer mean?
  3. count everything you allocate while running
  4. visited sets, frequency maps, stack frames
  5. input excluded; output is the ambiguous case

basics

~20 s

Auxiliary space counts only the extra memory an algorithm allocates while it runs; total space also counts the input it was handed. A scan that keeps two counters over a million-element input is O(1) auxiliary but O(n) total.

solid answer

~50 s

Auxiliary space is the memory an algorithm allocates on top of the input it was given; total space is auxiliary plus the input itself. When an interviewer asks for your space complexity they almost always mean auxiliary — otherwise every algorithm that merely reads an n-element input would be O(n) and the measure would say nothing about your solution. Auxiliary covers everything you create while running: a frequency map over event codes, a visited set, a memo table, a temporary buffer, and the frames of a recursive helper. The one genuinely ambiguous item is the output: a routine that must return an n-element result cannot avoid allocating it, so many people exclude the required output and report only what is spent beyond input and output. State which convention you are using and the ambiguity disappears.

go deeper

for a junior

Be ready to give both numbers for a simple scan: constant extra memory, linear total counting the input. Naming the distinction unprompted is what separates a rehearsed answer from an understood one.

for a middle

Explain why the input is excluded at all — it is a fixed cost every solution pays, so including it hides the difference between solutions. Then enumerate what does count: buffers, sets, maps, tables, call frames.

for a senior

Show that you size the bound against what actually drives it. A frequency map is O(distinct keys), not O(rows), and on a real stream that difference decides whether the job fits in memory or dies.

for a principal

Own the convention itself. When a team quotes space in design docs and reviews, insist the bound names its variable and states whether the output is counted, or the numbers stop being comparable across proposals.

## Two different measures Space complexity can be quoted two ways, and the two differ by exactly one term: - **Total space** — every cell of memory the algorithm occupies while running, *including the input it was handed*. - **Auxiliary space** — only what the algorithm allocates *beyond* that input. Because the input is a fixed cost that every solution to the same problem pays, total space is a poor way to compare solutions. Two candidates for the same task, one keeping a single running counter and one building a full copy of the data, would both be reported as O(n) total — which hides the entire difference between them. Auxiliary space separates them: O(1) versus O(n). That is why "what is your space complexity?" in an interview is, by default, a question about auxiliary space. ## What counts as auxiliary Anything the algorithm brings into existence: | What you allocate | Typical auxiliary cost | | --- | --- | | A handful of indices, counters, accumulators | O(1) | | A visited/seen set over the elements processed | O(n) | | A frequency map over distinct keys observed | O(k), k = distinct keys | | A one-dimensional table over positions | O(n) | | A two-dimensional table over two sequences | O(n·m) | | A temporary buffer merged back into the input | O(n) | | Recursive call frames | O(depth) | Two entries there are worth dwelling on. First, a frequency map is **not** automatically O(n): its size is bounded by the number of *distinct* keys, so counting how often each of 26 letters appears in a billion-character stream is O(1) auxiliary, while counting distinct session identifiers in the same stream is O(n) in the worst case. Quoting O(n) for both is a common sloppiness. Second, the call stack is real memory: a recursive routine that allocates nothing on the heap still spends one frame per level of depth, and that belongs in the space figure. ## The output question The honest ambiguity is whether the answer you must return counts. If the problem says "return a new array of n running averages", no solution can use less than O(n) for the result — so counting it makes the measure useless again, exactly as counting the input did. The common convention is therefore to *exclude the required output* and report the working memory beyond it. But conventions differ between textbooks, courses and interviewers, so the safe move is one clause: "O(1) auxiliary beyond the output array, which is itself O(n)." That sentence is correct under every convention and shows you know the distinction exists. An interviewer who wanted the other convention will simply agree. A related trap: a routine that *builds nothing* but returns a view, slice or index range into the input has not spent O(n) — it spent O(1). Whether your language of thought copies or aliases here matters, and interviewers do probe it. ## How to say it A complete answer names both numbers when they differ meaningfully: > "One pass, two accumulators, no allocation — O(1) auxiliary space, O(n) total counting the input." > "I keep a set of the identifiers seen so far, so O(n) auxiliary in the worst case where every row is distinct, and O(1) when the identifier alphabet is small." Note how the second answer states what the O(n) is a function of. "O(n) space" with no statement of what n counts is where most junior answers lose points, because the same routine can be O(1) or O(n) depending on whether n means rows read or distinct keys retained. ## Wrong turns to avoid - **"Reading the input costs space."** Reading costs time. The input already exists; you did not allocate it. - **"I only allocated one map, so it's O(1)."** The number of *objects* you create is irrelevant; the number of *cells* they hold is the measure. One map can be the biggest thing in the process. - **"Space is whatever is left after the function returns."** No — it is the peak occupancy during the run. A buffer that is discarded at the end still had to fit in memory while it existed. - **"Auxiliary means heap only."** Stack frames are memory on the same budget.

  • A routine counts how many times each event code appears in a billion-row stream. Is that O(n) auxiliary space?
    Only if the codes are unbounded. The map holds one entry per *distinct* code, so if the code alphabet has a few hundred fixed values the auxiliary space is O(1) regardless of stream length; if codes are arbitrary identifiers it is O(k) with k up to n. Always say what the n in your bound counts — rows processed and distinct keys retained are different quantities and here only the second one drives memory.
  • If a problem requires returning an n-element result, can you honestly claim O(1) space?
    You can claim O(1) *auxiliary beyond the required output*, and you should phrase it exactly that way. The output is forced by the problem statement, so charging it to your solution makes the measure unable to distinguish solutions — the same reason the input is excluded. Stating the convention in one clause is what interviewers are listening for; a bare "O(1)" invites the objection that you allocated an n-element array.
  • Does discarding a temporary buffer before returning lower your space complexity?
    No. Space complexity is peak occupancy during the run, not residue afterwards. A buffer of n cells had to fit in memory while it existed, so the routine is O(n) auxiliary even though nothing survives the return. This matters practically: the machine has to hold the peak, and a job that briefly doubles its footprint can fail on a memory ceiling it otherwise fits under comfortably.

Auxiliary space is the countertop you need to cook the meal; total space also charges you for the groceries you were already given.

saying these in an interview costs you the question

  • Says reading a large input costs the algorithm space
  • Quotes O(n) without saying what n counts
  • Claims one allocated map is O(1) because it is one object
  • Forgets recursive call frames are part of space
  • Reports space as what survives after the routine returns
  • Treats the required output as free without saying so

context

open as a page

Does mutating the caller's buffer make an algorithm in-place, or is more required?

level: middleimportance: must knowfreq 70%

basics

~20 s

More is required. In-place means O(1) auxiliary space — a constant amount of scratch memory, not zero. A routine that allocates a full-size temporary and copies it back over the caller's buffer is destructive, not in-place.

open as a page

When does memoizing a pricing calculation stop paying for the memory it consumes?

level: middleimportance: should knowfreq 50%

basics

~20 s

Memoization pays only when the same key is requested repeatedly. Its memory is distinct keys reached times bytes per entry, so a wide key space visited once each — every product-and-region pair in a nightly sweep — costs everything and saves nothing.

open as a page

How would you size the seen-set for deduplicating a billion-row clickstream, and what changes when it exceeds RAM?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Size it as distinct keys times realistic bytes per entry, not raw key bytes: a billion 16-byte keys lands in tens of gigabytes. Above RAM, partition by key hash, order the data so duplicates meet, or accept an approximate filter.

open as a page