AlgoFrames

Spot the bug: the tail runs off the end

Standard · Find the bug · about 8 min

This circular queue works until the tail reaches the last slot. After that, enqueue writes outside the array and rear returns the wrong value. Click the line with the bug, then fix it. Results follow the rules of "Implement a circular queue".

Example 1

Input:

[2,["enqueue 1","enqueue 2","dequeue","enqueue 3","rear"]]

Output:

[1,1,1,1,3]

The buggy code writes 3 into data[2], which a 2-slot array does not have.

Constraints

  • 1 ≤ k ≤ 1, 000
  • 1 ≤ ops.length ≤ 1, 000

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

More Queues: enqueue, dequeue; circular buffer problems