Queue Operation Simulator

Queue Simulation Operations Data Structures Halloween

Problem: Queue Operation Simulator

On Halloween night, a line of trick-or-treaters has formed at the local candy stand. Each trick-or-treater either arrives at the end of the line or leaves from the front. You are given an array of operations representing the events. There are two types of operations:

  • ENQUEUE x: A trick-or-treater with identifier x (which can be a number or string) joins the back of the queue.
  • DEQUEUE: The trick-or-treater at the front of the queue leaves (if the queue is not empty). If the queue is empty, ignore the operation.

Your task is to simulate these operations on an initially empty queue and return the final state of the queue as an array (from front to back).

Example:

For the input operations:

["ENQUEUE Alice", "ENQUEUE Bob", "DEQUEUE", "ENQUEUE Charlie", "ENQUEUE Dave", "DEQUEUE"]

The simulation would proceed as follows:

  1. ENQUEUE Alice → Queue: [Alice]
  2. ENQUEUE Bob → Queue: [Alice, Bob]
  3. DEQUEUE → Queue: [Bob]
  4. ENQUEUE Charlie→ Queue: [Bob, Charlie]
  5. ENQUEUE Dave → Queue: [Bob, Charlie, Dave]
  6. DEQUEUE → Queue: [Charlie, Dave]

Output: ["Charlie", "Dave"]

Implement the function to achieve this simulation.