Data Structures & AlgorithmsModerateProduct Company SDE · complexity round

Problem: A dynamic array doubles its capacity whenever it is full and copies all elements. Which statement about n pushes is correct?

  1. A. Each push is worst-case O(1) and amortized O(n)
  2. B. Each push costs O(log n) always
  3. C. Each push is amortized O(1), but a single push can cost O(n) during a resize
  4. D. Each push costs O(n) always

Correct answer: C. Each push is amortized O(1), but a single push can cost O(n) during a resize

Copies happen at powers of two, so the total copying work over n pushes is below 2n, giving amortized O(1). A single resizing push still takes O(n) time.

More Coding Interview Problems questions

Exams that ask this topic