Where it runs
1. The problem
A queue in a program has the same rule as a line at a checkout. A new person joins at the back, and the person at the front is served next. Whoever came first leaves first: first in, first out (FIFO for short). The stack you just built works the other way round.
You need a queue when order matters and nobody should wait longer than their turn. Print jobs wait like this, and so do tasks waiting for a free worker, requests to a server, and messages between systems. Later you will need a queue to walk a tree level by level. The idea is the same as with a stack: a small structure you can reach only by its rules. The difference is that you add and remove at different ends.
This step is part of the paid track
The first 33 steps are open to everyone: lesson, exercise and tests. This one opens with full access. Start with a free account and the free steps.
Create a free accountSee what is includedOn LeetCode
Problems where items are served in the order they arrived, such as requests in a time window or players taking turns. Add at the back of a queue and take from the front.
Add each call time at the back and drop calls older than 3000 ms from the front.
Build first in, first out from two stacks, pouring one into the other only when the output stack is empty.
Simulate the line, sending a student to the back when they skip the top sandwich.
Two queues of senator positions, and the winner of each round rejoins at the back with position plus n.