Chapter 4 · Arrays

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)

🔍Why amortized cost is O(1)
Resizes happen at sizes 1, 2, 4, 8, 16… Total copy work = 1+2+4+…+n = 2n. Spread over n pushes = 2 copies per push on average = O(1) amortized. The trick is that each doubling takes an exponentially longer time to fill again.

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

10
[0]
25
[1]
7
[2]
43
[3]
2
[4]
88
[5]
31
[6]
addremoveaccessmodify
idx:

→ Click an operation to see it in action

Internal pseudocode

dynamic-array.pseudo
1DynamicArray: { data[], size <- 0, capacity <- 1 }
2 
3push(value): // O(1) amortized
4 if size = capacity:
5 newData <- allocate(capacity x 2)
6 copy all elements to newData
7 free(data)
8 data <- newData
9 capacity <- capacity x 2
10 data[size] <- value
11 size += 1
12 
13pop(): // O(1)
14 if size = 0: error
15 value <- data[size - 1]
16 size -= 1
17 if size < capacity / 4: shrink(capacity / 2) // optional
18 return value
19 
20get(index): // O(1)
21 if index >= size: error
22 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

💡log n resizes for n pushes
Because capacity doubles each time, the number of resizes for n pushes is exactly log₂(n). For 1,000 pushes: only 10 resize events. For 1,000,000 pushes: only 20. This logarithmic resize count is why the amortized cost stays constant.

Time complexity

get(index) — direct address arithmeticO(1)

Constant time

push to end — amortized (O(n) worst per resize)O(1)

Constant time

pop from endO(1)

Constant time

insert at beginning or middle (shift elements)O(n)

Linear time

delete at beginning or middle (shift elements)O(n)

Linear time

🎯 Quick Check

A dynamic array has size=8, capacity=8. You call push() once. What is the capacity after this push?

🧠The one fact to internalize
Dynamic arrays give you O(1) amortized push because each element is only ever copied log n times in its lifetime — and log n is so small (20 for a million elements) that it effectively vanishes. Every modern language's default list is a dynamic array.

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.