AlgoFrames

Predict head and tail

Warm-up · Trace the algorithm · about 5 min

A circular queue has 4 slots, numbered 0 to 3. Follow enqueue 1, enqueue 2, enqueue 3, dequeue, dequeue, enqueue 4, enqueue 5, dequeue and predict where head and tail land. head is the slot of the oldest value; tail is the next free slot.

Example 1

Input:

{"capacity":4,"ops":["enqueue 1","enqueue 2","enqueue 3","dequeue","dequeue","enqueue 4","enqueue 5","dequeue"]}

Output:

head = 3, tail = 1

Constraints

  • The trace uses the input shown.

Learn the idea first: Queues: enqueue, dequeue; circular buffer, a visual lesson.

More Queues: enqueue, dequeue; circular buffer problems