Why dynamic arrays can append fast after resizing
Explain amortized append cost for dynamic arrays that grow by resizing.
A dynamic array starts with capacity 4 and currently holds 4 items. The next append triggers a resize before storing the fifth item. Amortized analysis: separate the cost of one expensive resize from the cost spread across a sequence of many appends. The novice move is to call the whole structure slow after seeing one resize spike, or to ignore the spike when a hard latency budget exists. Step 1 Append while capacity remains: store item in the next free slot and increment size. This is the cheap case that happens most of the time when capacity has slack. Step…
Sign up free — one personalized lesson every day, matched to your role and goals.
Already have an account? Sign in