Suppose we implement a stack using a dynamically allocated array. If the array fills, we are faced with a dilemma. From among the following choices, select the one that best describes our strategy for handling a filled array.
(a) We declare a new array, twice as big as the original, and copy the data into the new space for a total cost of O(n), over a sequence of n pushes.
(b) We declare another array and keep track of which of the two (or more) arrays contain the current top of the stack in a private member of the stack class. This costs us O(1) per push.
(c) For some fixed k, we create a new array of size n + 2^k and copy the data into the new space for an average cost of O(1) per push operation.
(d) We avoid implementing a stack using a dynamically allocated array, because it is inefficient to have to reallocate memory.
(e) None of these answers is a reasonable response.
I'm pretty sure the correct answer is a, but I dont understand why that would be the best one over the others? Are the others even practical? They seem ok to me. For example, c
is almost the same thing as `a, no? Why is doubling more advantageous then increasing by a constant amount? What about the other options-why dont they work?