All topics
52

A line: first in, first out

Tier 1Locked

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 included

On 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.

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy