Bounded FIFO Queue
Problem statement
Model a FIFO queue that starts empty and holds at most capacity integers. Process operations in order.
[0, value]tries to enqueue value at the back. Return 1 if it is accepted. If the queue is full, leave it unchanged and return 0.[1, 0]dequeues and returns the front value. If the queue is empty, leave it unchanged and return -1.
Return one integer per operation. The queue never grows. No concurrency or blocking behavior is required.
Function
fifoResults(capacity: int, operations: int[][]) → int[]Examples
Example 1
capacity = 2operations = [[0,4],[0,9],[0,7],[1,0],[1,0],[1,0]]return = [1,1,0,4,9,-1]4 and 9 fill the queue. The attempted enqueue of 7 is rejected. Dequeues return 4, then 9, then -1 because the queue is empty.
Example 2
capacity = 1operations = [[0,0],[1,0],[0,8],[1,0]]return = [1,0,1,8]The value 0 is valid data. Removing it frees the only slot, so 8 can then be enqueued and removed.
Example 3
capacity = 3operations = []return = []An empty operation batch produces an empty result array.
Constraints
1 <= capacity <= 10000.0 <= operations.length <= 10000.- Each operation has exactly two integers. Its type is 0 or 1. An enqueue value is from 0 through 1000000; a dequeue uses 0 as its second entry.