Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft

A dynamic array doubles its capacity when full, copying all elements. Starting from capacity 1, what is the total number of element copies caused by resizing while inserting 1,000 elements, and what is the amortised cost per insertion?

  1. A. 999 copies; O(log n) amortised
  2. B. 500,500 copies; O(n) amortised
  3. C. 2,047 copies; O(1) amortised
  4. D. 1,023 copies; O(1) amortised

Correct answer: D. 1,023 copies; O(1) amortised

Resizes happen at sizes 1, 2, 4, …, 512, copying 1 + 2 + … + 512 = 1,023 elements in total, less than 2n, so the amortised cost per insertion is O(1).

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic