Dynamic Arrays
Static arrays have a fixed size. Dynamic arrays appear unlimited — but internally they're just static arrays that secretly replace themselves with bigger ones when full. Understanding how this works unlocks amortized analysis.
How do dynamic arrays work?
A dynamic array wraps a fixed-size array and grows automatically. When it's full, it allocates a bigger block (usually double the size), copies the elements over and continues. That single copy costs O(n), but it happens rarely enough that push is O(1) amortized. JavaScript arrays, Python lists and Java's ArrayList all work this way.
class DynamicArray {
constructor() { this.capacity = 2; this.length = 0; this.data = new Array(2); }
push(value) {
if (this.length === this.capacity) { // full: double and copy
this.capacity *= 2;
const bigger = new Array(this.capacity);
for (let i = 0; i < this.length; i++) bigger[i] = this.data[i];
this.data = bigger;
}
this.data[this.length++] = value;
}
}The doubling strategy
When a dynamic array runs out of capacity, it allocates a new block of 2× the size, copies all elements, and frees the old block. This one-time O(n) cost is paid so rarely that the amortized cost per push stays O(1).
Start: cap=1, push(A)
Full! Resize to 2, push(B)
Full! Resize to 4, push(C)
push(D) — fits, no resize
Full! Resize to 8, push(E)
Live demo — array operations
Push and pop elements to see how a dynamic array manages its contents. Internally, each push that exceeds capacity triggers a doubling resize.
// Interactive Array — length: 7
→ Click an operation to see it in action
Internal pseudocode
1DynamicArray: { data[], size <- 0, capacity <- 1 }23push(value): // O(1) amortized4 if size = capacity:5 newData <- allocate(capacity x 2)6 copy all elements to newData7 free(data)8 data <- newData9 capacity <- capacity x 210 data[size] <- value11 size += 11213pop(): // O(1)14 if size = 0: error15 value <- data[size - 1]16 size -= 117 if size < capacity / 4: shrink(capacity / 2) // optional18 return value1920get(index): // O(1)21 if index >= size: error22 return data[index]
Shrinking strategy
Should you shrink when elements are deleted? The naive answer is “shrink at 50% full” — but this causes a worst-case thrash: push/pop at the boundary triggers a resize on every single operation.
✗ Shrink at 50% (naive — avoid)
If you push to exactly full, then pop, then push, then pop — every operation triggers a resize. O(n) per operation in this pathological case.
✓ Shrink at 25% (correct)
Only shrink when size falls below capacity/4. This ensures at least n/4 operations must happen between any two consecutive shrinks. Amortized O(1) guaranteed.
✓ Never shrink (also valid)
For short-lived arrays, the memory waste may be acceptable. Many standard library implementations simply never shrink. shrink_to_fit() exists as an explicit opt-in.
Real capacity growth
8
pushes
3 resizes
7 total copies
16
pushes
4 resizes
15 total copies
64
pushes
6 resizes
63 total copies
1000
pushes
10 resizes
1023 total copies
Time complexity
Constant time
Constant time
Constant time
Linear time
Linear time
🎯 Quick Check
A dynamic array has size=8, capacity=8. You call push() once. What is the capacity after this push?
Dynamic Arrays: summary
Key points
- Doubling (rather than adding a fixed amount) is what makes appends O(1) amortized.
- Capacity is how much is allocated; length is how much is used.
- Inserting at the front is still O(n).
Frequently asked questions
›Why does an array double its size instead of growing by one?
Growing by one would copy every element on every push, which is O(n²) overall. Doubling means copies get rarer as the array grows, averaging O(1) per push.