Serving a queue

Simulate a queue at a counter. join NAME adds a person to the back; serve serves the person at the front and prints their name, or prints EMPTY if nobody is waiting.

What to build

A queue that follows commands.

Requirements

  • join NAME adds to the back.
  • serve prints the name at the front, or EMPTY.

Input

The first line has n (1 to 1000). Each of the next n lines is join NAME or serve.

Output

One line for each serve command.

Constraints

n is from 1 to 1000.

Example

Input:

4
join Rahim
join Nadia
serve
serve

Output:

Rahim
Nadia

Why it works

A deque adds at one end and removes at the other, each in constant time, which matches how a real line works.

Examples

Example 1 · Input
4
join Rahim
join Nadia
serve
serve
Expected output
Rahim
Nadia
Example 2 · Input
1
serve
Expected output
EMPTY

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

queue.append(name) joins the back; queue.popleft() serves the front.

Hint 2

Check if queue: before serving.

Solution

Show one correct solution (try it yourself first)
from collections import deque

queue = deque()
n = int(input())
for _ in range(n):
    parts = input().split()
    if parts[0] == "join":
        queue.append(parts[1])
    elif queue:
        print(queue.popleft())
    else:
        print("EMPTY")

Lesson: Stacks and queues →