AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started

Topic

Queue coding problems

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.

New to queue? Read the Queues and Deques tutorial first.

27 problems · 6 easy · 13 medium · 8 hard · judged by hidden tests in 13 languages

27problems

6 easy

13 medium

8 hard

Sign in to tick off what you solve.

Study plan: 14 problems · about 8 h 10 min

Start the study plan

Next up: First Unique Character in a String

How it works

Queue, step by step

Round-robin scheduling, with a queue

tasks = A:3, B:5, C:2, D:4; quantum = 2

queueA:3B:5C:2D:4frontCPU0donenone yetclock = 0 · quantum = 2

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.

0:01 / 0:32Step 1 of 9
Transcript · 9 steps

Study plan

Learn queue in 8 days

14 of the 27 Queue problems (4 easy, 7 medium and 3 hard) over 8 days, about 8 h 10 min in all — the pattern first, then easiest to hardest. After that, the other 13 in the full list below are practice at your own pace. Then move on to Monotonic Stack.

  1. Day 1

    about 1 h 15 min

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

  2. Day 2

    about 55 min

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

  3. Day 3

    about 1 h 10 min

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

  4. Day 4

    about 1 h 10 min

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

  5. Day 5

    about 1 h 10 min

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

  6. Day 6

    about 50 min

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

  7. Day 7

    about 50 min

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

  8. Day 8

    about 50 min

    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 Coding Problems: 27 Questions with Solutions

27 queue coding problems — 6 easy · 13 medium · 8 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 8-day plan.

  • Problems: 27
  • By difficulty: 6 easy · 13 medium · 8 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.

New to queue? Read the Queues and Deques tutorial first.

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

14 of the 27 Queue problems (4 easy, 7 medium and 3 hard) over 8 days, about 8 h 10 min in all — the pattern first, then easiest to hardest. After that, the other 13 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

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

Day 6

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

Day 7

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

Day 8

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 (6)

Medium (13)

Hard (8)

Companies that ask queue problems

Next topic: Monotonic Stack