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
tasks = A:3, B:5, C:2, D:4; 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- First Unique Character in a String Easy
- Time Needed to Buy Tickets Easy
- Number of Students Unable to Eat Lunch Easy
Day 2
Medium problems: the same pattern with one twist each. Name the twist before you code.
- Reveal Cards In Increasing Order Medium
- Dota2 Senate Medium
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.
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 orshift()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
- Time Needed to Buy Tickets: a queue, then the arithmetic behind it.
- Reveal Cards In Increasing Order: simulating positions, not cards.
- Dota2 Senate: two queues taking turns.
All queue problems
Easy (3)
- Number of Students Unable to Eat Lunch Array, Stack, Simulation
- Time Needed to Buy Tickets Array, Simulation
- First Unique Character in a String Hash Table, String, Counting
Medium (5)
- Find the Winner of the Circular Game Array, Math, Recursion
- Number of People Aware of a Secret Dynamic Programming, Simulation
- Dota2 Senate String, Greedy
- Reveal Cards In Increasing Order Array, Sorting, Simulation
- Maximum Sum Circular Subarray Array, Divide and Conquer, Dynamic Programming
Hard (4)
- Constrained Subsequence Sum Array, Dynamic Programming, Sliding Window
- Maximum Number of Tasks You Can Assign Array, Binary Search, Greedy
- Count Subarrays With Fixed Bounds Array, Sliding Window, Two Pointers
- Maximum Number of Robots Within Budget Array, Sliding Window, Two Pointers
Companies that ask queue problems
- Amazon 12 problems on queue
- Google 8 problems on queue
- Microsoft 3 problems on queue
- Adobe 2 problems on queue
- Flipkart 2 problems on queue
- Meta 2 problems on queue
- Bloomberg 1 problem on queue
- Databricks 1 problem on queue
- Goldman Sachs 1 problem on queue
- Sprinklr 1 problem on queue
Next topic: Monotonic Stack