Growing Circular Queue
Problem statement
Implement a FIFO queue that starts empty with capacity 10 and preserves every value during growth. Process the given operations in order. Each operation is a pair [type, value].
[0, value]: produce a value at the back and return the new size. If the queue is full, double its capacity before insertion.[1, 0]: consume and return the front value, or return-1when empty.[2, 0]: return the current size.[3, 0]: return the current capacity.
Return one integer result for each operation. Capacity never shrinks. Use a resizable circular buffer so that insertion and removal take amortized constant time.
Function
processQueue(operations: int[][]) → int[]Examples
Example 1
operations = [[0,7],[0,8],[1,0],[2,0],[3,0],[1,0],[1,0]]return = [1,2,7,1,10,8,-1]The two insertions return sizes 1 and 2. The first removal returns 7, leaving size 1 and capacity 10. The next removal returns 8; removing from the empty queue returns -1.
Example 2
operations = [[3,0],[0,0],[1,0],[3,0]]return = [10,1,0,10]Capacity starts at 10. Inserting the value 0 returns size 1, and consuming it returns 0. Removing a value does not shrink capacity.
Constraints
0 <= operations.length <= 10000.- Every operation has exactly
2integers. - Every integer in
operationsis between0and1000000000, inclusive. - The first integer is one of
0,1,2, or3. For types other than0, the second integer is0.