Skip to interview prep
Apexed

67. Why does a dynamic array that doubles its capacity give O(1) amortized append, while growing by a fixed amount gives O(n)?

MediumAlgorithm analysis

Technical

Type or dictate your answer. Your draft is saved on this device.

Show model answer

With doubling, a resize copies all current elements, but resizes happen at sizes 1, 2, 4, ..., so the total copying over n appends is at most 1 + 2 + 4 + ... + n < 2n. Adding the n writes gives under 3n work for n appends: O(1) amortized per append, even though one individual append can cost O(n).

If capacity grows by a constant c instead, a copy happens every c appends and copies roughly c, 2c, 3c, ... elements, which sums to about n²/(2c). That is O(n) amortized per append. Any constant growth factor above 1 (such as 1.5) keeps the amortized cost constant; it trades memory waste against copy frequency.

What interviewers listen for

  • Geometric growth makes total copying linear
  • Constant growth makes it quadratic
  • Amortized is not average-case: it is a worst-case bound over a sequence

Example program

Why does a dynamic array that doubles its capacity give O(1) amortized append, while growing by a fixed amount gives O(n) — Data structures & algorithms intervi