GATE Computer ScienceVery hardGATE CS (PYQ pattern)

A dynamic array has capacity 1 initially. An append to a full array doubles the capacity (copying all n current elements); after a deletion, if the size is at most a quarter of the capacity (and the capacity exceeds 1), the capacity is halved (copying all n elements). The operation sequence is 9 appends followed by 10 repetitions of (delete, append). How many element copies are made in total?

  1. A. 8
  2. B. 23
  3. C. 15
  4. D. 175

Correct answer: C. 15

The 9 appends cost 1 + 2 + 4 + 8 = 15 copies (at sizes 1, 2, 4, 8). The size then oscillates between 9 and 8 with capacity 16, far above the quarter threshold, so there are no further copies. Shrinking at half capacity would cause a copy on almost every operation (175 here).

More Algorithms questions

Exams that ask this topic