Interview Questions

How do implement Circular Queue?

Software QA/Tests Interview Questions from Microsoft


(Continued from previous question...)

How do implement Circular Queue?

Question:
How do implement Circular Queue?


maybe an answer:


Array is enough with head, and tail position indexes (initially or when empty both be -1), the notion of circular is that the queue start and end positions could be anywhere in the array. Then basic operations are implemented as:
- enQueue: if queue is not full, add the element to tail = (tail + 1) % QUEUE_ARRAY_SIZE
- deQueue: if not empty, take element at head, and head = (head + 1) % QUEUE_ARRAY_SIZE
Note: For the case of full, you might need to re-size your array to a bigger size.
For the first time at enQueue you might need to point head to what tail points to.
In deQueue if no more element in queue set head and tail to -1

(Continued on next question...)

Other Interview Questions