Queue Coding Problems: 12 Questions with Solutions

12 queue coding problems — 3 easy · 5 medium · 4 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 7-day plan.

  • Problems: 12
  • By difficulty: 3 easy · 5 medium · 4 hard
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
  • Cost: Free on every plan; sign in to run and submit

First in, first out: the structure behind breadth-first search, task scheduling, sliding-window maximums (as a deque) and any process where things are served in arrival order. The problems here practise the queue's own operations and the circular buffer that implements it in fixed space.

How queue works, step by step

queueemptyCPUABCDABDB024689111314doneCt=6At=9Dt=13Bt=14clock = 14 · quantum = 2
Round-robin scheduling, with a queue. Example: tasks = A:3, B:5, C:2, D:4; quantum = 2
  1. Round robin shares one processor fairly: each task runs for at most 2 time units, then goes to the back of the line. A queue is that line (take from the front, add at the back), so the order of turns falls straight out of first in, first out.
  2. A runs from t=0 to t=2 and still needs 1, so it goes to the back of the queue behind B, C and D. It will not run again until each of them has had a turn.
  3. B runs from t=2 to t=4 and still needs 3, so it goes to the back of the queue behind C, D and A. It will not run again until each of them has had a turn.
  4. C runs from t=4 to t=6 and is finished, so it leaves the queue for good. D is now at the front and runs next.
  5. D runs from t=6 to t=8 and still needs 2, so it goes to the back of the queue behind A and B. It will not run again until each of them has had a turn.
  6. A runs from t=8 to t=9, needing only 1 of its 2, and is finished, so it leaves the queue for good. B is now at the front and runs next.
  7. B runs from t=9 to t=11 and still needs 1, so it goes to the back of the queue behind D. It will not run again until D has had a turn.
  8. D runs from t=11 to t=13 and is finished, so it leaves the queue for good. B is now at the front and runs next.
  9. B runs its last unit, t=13 to t=14, and the queue is empty: C finished at 6, A finished at 9, D finished at 13 and B finished at 14. Each turn is one O(1) dequeue plus at most one enqueue, so the schedule costs O(turns), 8 here.

Queue study plan

11 of the 12 Queue problems (3 easy, 5 medium and 3 hard) over 7 days, about 6 h 40 min in all — the pattern first, then easiest to hardest. After that, the other 1 in the full list below are practice at your own pace. Then move on to Monotonic Stack.

Day 1

Learn the pattern: read the essentials and step through the walkthrough above, then solve these 3.

Day 2

Medium problems: the same pattern with one twist each. Name the twist before you code.

Day 3

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 4

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 5

Hard problems: the pattern combined with a second idea. Give each a full attempt before reading the editorial.

Day 6

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Day 7

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Next topic: Monotonic Stack

Queue: the essentials

When to reach for it

Items served in arrival order, some sent back to the end — a ticket line, round-robin turns, cards dealt with the next one moved to the bottom, senators who each act in turn. Whenever the statement says "goes to the back of the line", model it with a queue. A double-ended queue opens the other end as well, which is what the sliding-window maximum and 0-1 BFS need.

The pattern

Load the initial order, then loop: take from the front, apply the rule, and put the item at the back if it is still in play. When the loop would run too long, find the arithmetic the queue performs. In Time Needed to Buy Tickets, everyone up to position k buys at most tickets[k] tickets before k is done and everyone behind at most tickets[k] - 1, so one pass of min gives the answer.

from collections import deque

def deck_revealed_increasing(deck):
    order = deque(range(len(deck)))     # positions, in reveal order
    out = [0] * len(deck)
    for card in sorted(deck):
        out[order.popleft()] = card
        if order:
            order.append(order.popleft())  # next card goes to the bottom
    return out

Cost

O(1) per enqueue and dequeue on a real queue, so a simulation costs O(number of operations); a circular buffer of fixed capacity uses O(capacity) space.

Common mistakes

  • Dequeuing with list.pop(0) in Python or shift() in JavaScript, which can cost O(n) each time.
  • In a circular buffer, confusing full with empty when head == tail; keep a count, or leave one slot unused.
  • Simulating every round when the counts are large, instead of computing the rounds directly.
  • Re-queueing an item after its last action.

Start with

All queue problems

Easy (3)

Medium (5)

Hard (4)

Companies that ask queue problems

Next topic: Monotonic Stack