Adding to a dynamic array is O(1) amortized even though some inserts trigger an O(n) resize. Explain why, and how you'd justify that bound to a teammate who only sees the occasional slow append.

technical-conceptual · Mid level · software-engineering

What the interviewer is really asking

Assesses whether the candidate understands amortized analysis as a real reasoning tool — that a cost spread over a sequence differs from worst-case-per-op — and can derive the doubling argument rather than just assert O(1).

What to say

What to avoid

Example answers

Strong: I'd walk them through the geometric series: doubling means the copy costs are n, n/2, n/4, ... which sum to under 2n total for n inserts, so the per-insert average is constant. The accounting view is cleaner for intuition — charge every push 3 'tokens', one pays for placing the element, the other two bank toward copying it and one already-copied element at the next resize, so the bank never goes negative.

Weak: Appending to a list is just O(1), the language handles it, so I wouldn't worry about the resize.

Want questions matched to your role? Paste a job title, job description, or CV and get a personalized set, or go Pro to unlock the full bank.