Skip to the solution
MediumDSA11 min read

What is amortized analysis?

195 views

The problem

Question presented to candidate: "Appending to a dynamic array sometimes has to copy every element into a bigger block, which is O(n). Yet everyone says append is O(1). Explain how both can be true, what amortized analysis is, and show it for the Python structures you would use."

What a strong answer should cover:

  • Amortized cost = total cost of a worst-case sequence of n operations, divided by n; it is a guarantee, not an average over random inputs.
  • The doubling argument: copies 1 + 2 + 4 + … < 2n, so n appends cost under 3n β€” O(1) amortized, while a single append is still O(n) in the worst case.
  • Why the growth must be geometric: growing by a constant amount gives O(nΒ²) total; shrinking must leave a gap (shrink at ΒΌ full, not Β½) or it thrashes.
  • The three methods by name (aggregate, accounting/banker, potential) and another example such as a queue built from two stacks.
  • Python specifics: list.append is amortized O(1) while list.pop(0) is O(n) every call; CPython over-allocates by roughly an eighth, which is still a geometric growth factor.

Clarifying questions expected:

  • "Do we care about the worst case of a single operation (latency), or only total throughput?"
  • "Do we know the final size in advance, so we could pre-allocate?"

Code / implementation expected: Optional β€” a Python dynamic array or a two-stack queue that counts copies, with the amortized argument stated.

  • amortized

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

11 min read Β· 13 sections

Python code

Edit the code and run it here. Each printed line with an expected value becomes a test.

PythonDynamic array: cost of every push, doubling vs growing by a constant
PythonQueue from two stacks: one slow dequeue, O(1) amortized
PythonWhy shrinking at half full breaks the bound

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 115 of 154 in DSA

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All DSA questions
Up nextMedium

What is the two-pointer technique?

Continue