Queue — Enqueue and Dequeue
Practice this coding question on Coding Gurukul.
Problem statement
Implement a queue that starts empty. - ENQUEUE x: add x at the back. - DEQUEUE: remove and print the front value. - FRONT: print the front value without removing it. - SIZE: print the number of values. If DEQUEUE or FRONT is called on an empty queue, print EMPTY. ENQUEUE produces no output. Values are removed in the order they were added. Support each command in O(1) amortized time. Worked example After adding 4 and 8, FRONT prints 4. DEQUEUE removes and prints 4. Adding 2 leaves [8, 2], so FRONT prints 8 and SIZE prints 2.
Input format
First line: q. Next q lines: ENQUEUE x, DEQUEUE, FRONT, or SIZE.
Output format
One line for every DEQUEUE, FRONT or SIZE.
Constraints
1 <= q <= 100000; -10^9 <= x <= 10^9. At least one output command.
Sample input
7 ENQUEUE 4 ENQUEUE 8 FRONT DEQUEUE ENQUEUE 2 FRONT SIZE
Sample output
4 4 8 2
Sign in to run or submit a solution in C++, Java or Python.