Data Structures
Queue
enqueue: O(1) · dequeue: O(1) with the right structure · front: O(1)
The idea, in plain English
A queue is a line at a coffee shop. The first person to arrive is the first person served. This rule is called FIFO, short for First In, First Out. New people join the back of the line. Served people leave from the front.
How it works
- 1enqueue: a new item joins the back of the line.
- 2dequeue: the item at the front is served and leaves.
- 3front: check who is next without serving them.
When you'd use it
Use a queue for anything handled in the order it arrives, like print jobs, support tickets, or tasks waiting to run.
Common beginner mistakes
- Don't mix up the ends. You add items to the back and remove them from the front.
- In JavaScript, array.shift() is slow on a huge list. A real queue design avoids this problem.
Try it — edit and run
Click the code to edit · press ⌘/Ctrl+↵ to run
Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.
Front: a
Serve: a
Serve: b
Waiting: 1Note: Python's collections.deque is the right tool for a queue. Removing from the front is fast, unlike with a plain list.
See it in motion
Ready. Press play — enqueue joins the back, dequeue serves the front.
Not sure this is the right topic? See the learning paths → or where this leads →