The Plain Picture#
Your phone plan costs ₹0 for 29 days and ₹3,000 on the 30th. Is it an expensive phone plan? Per day, it's ₹100 — the occasional big payment, spread over all the days, is small.
The Real Name#
Amortized cost = the average cost per operation over a long sequence, even when a few individual operations are expensive.
The classic example is ArrayList.add. An ArrayList is an array underneath (Java Basics Ch 8 — arrays can't grow). When it's full,
add creates a bigger array (Java's ArrayList grows by 1.5×) and copies every element into it — an O(n) step. Yet add is described as
amortized O(1).
Under the Hood#
Simulating Java's growth rule (start at capacity 10, grow 1.5× when full) for a million adds:
ArrayList-style growth: 1,000,000 adds caused 2430972 element copies (2.43 per add)Why the total stays proportional to n: the copies happen at sizes 10, 15, 22, 33, …, up to n. Each is 1.5× the previous, so they form a geometric series: the total is at most a small constant times the final size (for growth factor 1.5 the sum is under 3n). A million adds cost about 2.4 million copies — about 2.4 copies per add, forever, no matter how big the list gets. So each add is O(1) on average, while a single unlucky add is O(n).
cost of each add:
│ █
│ █
│ █
│ █
│ █
│ █ █
│▁▁▁█▁▁█▁▁█▁▁▁▁▁█▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁ ← mostly cost 1; rare spikes
└───────────────────────────────────────────────────────────▶ add number
spikes get bigger but rarer; the average stays constantWhy growing by a constant factor matters: if the list grew by a fixed +10 slots each time instead, it would copy at sizes 10, 20, 30, … — a
total of about n²/20 copies: O(n) per add, O(n²) overall. Multiplying, not adding, is what makes it amortized O(1). The same idea explains
StringBuilder (Classes, Objects and Memory Ch 7) and HashMap resizing (Collections, From the Inside).
Tip: if you know the final size, pre-size it — new ArrayList<>(n), new StringBuilder(n) — to skip the copies entirely.
Check yourself
3 questionsWhat does "amortized O(1)" mean?
Show answerHide answer
Averaged over a long sequence of operations, each costs constant time, even though a few individual ones are expensive.
Why is a single ArrayList.add sometimes O(n)?
Show answerHide answer
When the backing array is full, it allocates a larger array and copies every element.
Why must the array grow by a factor (like 1.5× or 2×) rather than by a fixed amount?
Show answerHide answer
Factor growth makes the copying cost a geometric series summing to O(n) total; fixed-size growth makes it O(n²) total.