Comments on: Implementing a Queue Using an Array https://www.happycoders.eu/algorithms/implement-queue-using-array/ Wed, 27 Nov 2024 13:35:40 +0000 hourly 1 By: Sven Woltmann https://www.happycoders.eu/algorithms/implement-queue-using-array/#comment-17724 Tue, 14 Mar 2023 09:35:32 +0000 https://www.happycoders.eu/?p=29900#comment-17724 Hi Zicheng,

Shrinking can be implemented similar to growing. You would override the dequeue() method, and at its end, you would check if the number of elements is significantly lower than the array size (e.g. one quarter). If so, call a shrink() method.

The shrink() method would then create a new array (e.g. half the size of the existing array, so there's room left to add elements) and copy the existing elements from the old array to the new array (this would also need *two* calls to System.arrayCopy() in case the elements wrap around the end of the array).

Best wishes
Sven

]]>
By: Zicheng Liu https://www.happycoders.eu/algorithms/implement-queue-using-array/#comment-17702 Sat, 04 Mar 2023 11:12:18 +0000 https://www.happycoders.eu/?p=29900#comment-17702 Hi Sven,

I hope this message finds you well. Thank you very much for taking the time to share your insightful explanation here.

I had an interview earlier today where I encountered two questions related to the bounded and unbounded array implementation, and I was able to provide a similar answer to what you had discussed in your article. However, the interviewer specifically asked about the scenario of size shrinking in the unbounded array implementation, such as when the array grows to a size of 2*10^8 while there are only two elements in the queue. I noticed that this particular topic was not covered in your article. If it is not too much trouble, could you kindly share your thoughts on this question as well?

]]>