Build A Queue

Build A Queue

Process a sequence of queue operations and return the values produced by the dequeues, in order. Operations are:

  • e<x> — enqueue the integer x (e.g. e5).
  • d — dequeue (remove and emit the front value).

A d on an empty queue emits nothing. A queue is first-in, first-out.

buildQueue(ops)

Input format: space-separated operations. Output the dequeued values, space-separated.

Examples

[e1, e2, d, e3, d, d]   // → [1, 2, 3]
[e5, d, d]              // → [5]
[d]                     // → []

Walkthrough

Thinking it through

Two smaller problems glued together: parsing operations, and simulating a first-in-first-out queue as they're applied. Neither is complicated alone — the value is in keeping the simulation clean.

Understanding the FIFO contract

Whatever was enqueued first comes out first — a checkout line. Enqueue adds to the back; dequeue removes from the front. That's the opposite of a stack (last in, first out). Getting the two confused is the most common way to fail this: enqueue → back, dequeue → front.

Building the simulation

  1. Keep an ordered collection for the queue's current contents, and a separate list recording every dequeued value in order — that list is the answer.
  2. Walk the operations in order.
  3. On enqueue, parse the number and add it to the back.
  4. On dequeue, check if the queue has anything. Empty means no-op. Otherwise remove the front value and append it to the output.
  5. After all operations, the output list is exactly the dequeued sequence.

Why the empty-queue check matters

A dequeue on an empty queue is a real input scenario, not an error — a no-op. Skipping the check would crash, or silently record undefined if using an array. Explicitly checking keeps the simulation honest.

Why this is the right level of complexity

Only enqueue-at-back and dequeue-from-front are needed, so a plain ordered collection processed linearly is exactly right — no sorting, no searching. n operations cost O(n) time, O(n) space for the recorded output.

main.py
Console
Press Run to execute your code, or Submit to test and complete this problem.