Design a restaurant order queue that serves priority orders before normal orders

Quick Overview

Design a restaurant order queue in which priority orders are always served before normal orders and each category keeps first-in, first-out order. It tests choosing queue structures with constant-time operations and handling an empty queue and priority orders that arrive after normal ones.

Design a restaurant order queue that serves priority orders before normal orders

Company: GoTo

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design a restaurant order queue that processes orders based on their priority level. The queue maintains two categories of orders: priority orders and normal orders. All priority orders must be processed before any normal orders. Within each category, orders are processed in FIFO (first-in, first-out) order based on the time they were enqueued. If the queue is empty when `dequeue` is called, return `-1`. Implement a `RestaurantOrderQueue` class with the following methods: - `RestaurantOrderQueue()` creates an empty queue. - `enqueue(orderId: int, isPriority: bool)` adds an order with the given `orderId` to the queue, marking it as priority or normal based on `isPriority`. - `dequeue() -> int` removes and returns the next order to process. Priority orders are returned before normal orders, and within each category FIFO order is preserved. It returns `-1` if the queue is empty. Example 1: ```text Input: ["RestaurantOrderQueue", "enqueue", "enqueue", "enqueue", "dequeue", "dequeue", "dequeue", "dequeue"] [[], [1, false], [2, true], [3, false], [], [], [], []] Output: [null, null, null, null, 2, 1, 3, -1] Explanation: queue = RestaurantOrderQueue() # empty queue queue.enqueue(1, False) # normal: [1], priority: [] queue.enqueue(2, True) # normal: [1], priority: [2] queue.enqueue(3, False) # normal: [1, 3], priority: [2] queue.dequeue() # -> 2 (priority served first) queue.dequeue() # -> 1 (FIFO normal) queue.dequeue() # -> 3 (FIFO normal) queue.dequeue() # -> -1 (empty queue) ``` Example 2: ```text Input: ["RestaurantOrderQueue", "enqueue", "enqueue", "dequeue", "enqueue", "enqueue", "dequeue", "enqueue", "dequeue", "dequeue", "dequeue", "dequeue"] [[], [5, false], [7, false], [], [9, true], [4, true], [], [8, false], [], [], [], []] Output: [null, null, null, 5, null, null, 9, null, 4, 7, 8, -1] ``` With no priority orders waiting, the first dequeue returns the oldest normal order, 5. Orders 9 and 4 arrive as priority orders after 7 is already waiting, and both are still served before it. Order 9 comes out before 4 because it arrived first; the ID value plays no part. Once the priority orders are gone, the normal orders 7 and 8 leave in arrival order, and the last call finds the queue empty. ```hint What decides the next order Within one category the order never changes after an order is added. Ask which orders you actually need to look at to decide what comes out next, and whether you can reach them without scanning. ``` ### Constraints & Assumptions - `1 <= orderId <= 10^9` - `isPriority` is a boolean value. - At most 10,000 calls are made to `enqueue` and `dequeue` combined. - `dequeue` returns `-1` when the queue is empty. ### Clarifying Questions to Ask - Can the same order ID be enqueued more than once, and should it then be served once per enqueue? - Will there ever be more than two priority levels? - Do several threads add and take orders at the same time, for example waiters adding orders while the kitchen takes them? ### What a Strong Answer Covers - A structure that keeps arrival order within each category and makes both `enqueue` and `dequeue` constant time. - The dequeue rule: priority orders first, then normal orders, and `-1` only when both categories are empty. - Why removing from the front of a plain array, or ordering by ID, costs more time or breaks FIFO. - Time and space complexity of each operation. - Edge cases such as an empty queue and a priority order that arrives after normal orders are already waiting. ### Follow-up Questions - Normal orders can wait forever while priority orders keep arriving. How would you bound their wait? - How would you support any number of priority levels while keeping FIFO within each level? - How would you add `cancel(orderId)` without making `dequeue` slow? - What changes if several threads call `enqueue` and `dequeue` at once?

Overview: Design a restaurant order queue in which priority orders are always served before normal orders and each category keeps first-in, first-out order. It tests choosing queue structures with constant-time operations and handling an empty queue and priority orders that arrive after normal ones.

GoTo logo
GoTo
Nov 25, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a restaurant order queue that processes orders based on their priority level. The queue maintains two categories of orders: priority orders and normal orders.

All priority orders must be processed before any normal orders. Within each category, orders are processed in FIFO (first-in, first-out) order based on the time they were enqueued.

If the queue is empty when dequeue is called, return -1.

Implement a RestaurantOrderQueue class with the following methods:

  • RestaurantOrderQueue() creates an empty queue.
  • enqueue(orderId: int, isPriority: bool) adds an order with the given orderId to the queue, marking it as priority or normal based on isPriority .
  • dequeue() -> int removes and returns the next order to process. Priority orders are returned before normal orders, and within each category FIFO order is preserved. It returns -1 if the queue is empty.

Example 1:

Input:
["RestaurantOrderQueue", "enqueue",  "enqueue", "enqueue",  "dequeue", "dequeue", "dequeue", "dequeue"]
[[],                     [1, false], [2, true], [3, false], [],        [],        [],        []]

Output: [null, null, null, null, 2, 1, 3, -1]

Explanation:
queue = RestaurantOrderQueue()        # empty queue

queue.enqueue(1, False)               # normal: [1], priority: []
queue.enqueue(2, True)                # normal: [1], priority: [2]
queue.enqueue(3, False)               # normal: [1, 3], priority: [2]
queue.dequeue()                       # -> 2    (priority served first)
queue.dequeue()                       # -> 1    (FIFO normal)
queue.dequeue()                       # -> 3    (FIFO normal)
queue.dequeue()                       # -> -1   (empty queue)

Example 2:

Input:
["RestaurantOrderQueue", "enqueue", "enqueue", "dequeue", "enqueue", "enqueue", "dequeue", "enqueue", "dequeue", "dequeue", "dequeue", "dequeue"]
[[], [5, false], [7, false], [], [9, true], [4, true], [], [8, false], [], [], [], []]

Output: [null, null, null, 5, null, null, 9, null, 4, 7, 8, -1]

With no priority orders waiting, the first dequeue returns the oldest normal order, 5. Orders 9 and 4 arrive as priority orders after 7 is already waiting, and both are still served before it. Order 9 comes out before 4 because it arrived first; the ID value plays no part. Once the priority orders are gone, the normal orders 7 and 8 leave in arrival order, and the last call finds the queue empty.

Constraints & Assumptions

  • 1 <= orderId <= 10^9
  • isPriority is a boolean value.
  • At most 10,000 calls are made to enqueue and dequeue combined.
  • dequeue returns -1 when the queue is empty.

Clarifying Questions to Ask Guidance

  • Can the same order ID be enqueued more than once, and should it then be served once per enqueue?
  • Will there ever be more than two priority levels?
  • Do several threads add and take orders at the same time, for example waiters adding orders while the kitchen takes them?

What a Strong Answer Covers Guidance

  • A structure that keeps arrival order within each category and makes both enqueue and dequeue constant time.
  • The dequeue rule: priority orders first, then normal orders, and -1 only when both categories are empty.
  • Why removing from the front of a plain array, or ordering by ID, costs more time or breaks FIFO.
  • Time and space complexity of each operation.
  • Edge cases such as an empty queue and a priority order that arrives after normal orders are already waiting.

Follow-up Questions Guidance

  • Normal orders can wait forever while priority orders keep arriving. How would you bound their wait?
  • How would you support any number of priority levels while keeping FIFO within each level?
  • How would you add cancel(orderId) without making dequeue slow?
  • What changes if several threads call enqueue and dequeue at once?