Data Structures & AlgorithmsHardTCS NQT · Product Company SDE

Problem: The dynamic array doubles its capacity whenever it is full, starting from capacity 1. After inserting 9 elements one by one, how many element copies occur in total during resizes (copying the existing elements at each resize)?

  1. A. 7
  2. B. 31
  3. C. 15
  4. D. 8

Correct answer: C. 15

Resizes occur when the array is full at sizes 1, 2, 4, 8, copying 1 + 2 + 4 + 8 = 15 elements before the 9th insert fits. Missing the resize at 8 gives 7, one extra doubling gives 31, and 8 counts only the last copy.

More Coding Interview Problems questions

Exams that ask this topic