A parcel-sorting conveyor uses a fixed-capacity circular buffer that behaves as a FIFO queue. The buffer can hold at most C parcels at once. You are given Q operations to apply in order, each one of:
ENQUEUE x - if the buffer already holds C parcels, print FULL and change nothing; otherwise append parcel value x to the back and print OK.DEQUEUE - if the buffer is empty, print EMPTY; otherwise remove the front parcel and print its value.FRONT - print the value of the front parcel, or EMPTY if the buffer is empty.REAR - print the value of the most recently enqueued parcel still in the buffer, or EMPTY if the buffer is empty.SIZE - print the number of parcels currently in the buffer.Line 1: two integers C and Q.
Next Q lines: one operation each, in one of the forms above.
Print exactly Q lines: the result of each operation, in order.
ENQUEUE x: -1000000000 <= x <= 1000000000Example 1
Input
2 5 ENQUEUE 7 ENQUEUE 9 ENQUEUE 4 FRONT DEQUEUE
Expected
OK OK FULL 7 7
Explanation
Capacity is 2. The first two enqueues succeed (OK, OK). The third enqueue hits capacity, so it prints FULL and is ignored. FRONT reports 7 (the oldest parcel). DEQUEUE removes and prints 7.
Example 2
Input
3 4 DEQUEUE ENQUEUE 5 REAR SIZE
Expected
EMPTY OK 5 1
Explanation
DEQUEUE on an empty buffer prints EMPTY. ENQUEUE 5 prints OK. REAR reports 5 (the newest parcel). SIZE reports 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →