Lesson 10 of 30
The checkout-line rule, turned into code: whoever got in line first gets served first.
8 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
This topic is about judgment and practical tradeoffs more than a single right answer — a mock interview is the better way to test yourself on it.
Be interviewed on this →Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
Picture the checkout line at a grocery store. The first person to join the line is the first person to get served. Everyone else waits their turn, joining at the back and moving forward as people ahead of them leave.
That's a queue: FIFO, first in, first out. It's the mirror image of the stack you just met. A stack serves the most recent arrival first; a queue serves the earliest arrival first.
Queues show up anywhere "fairness by order of arrival" matters. A printer handling documents in the order they were sent. Customer support tickets answered in the order they came in. A to-do list of background tasks a program needs to work through, one at a time, in the order they were added.
Adding to a queue is usually called enqueue ("join the line"). Removing from the front is called dequeue ("get served and leave"). New people always join at the back; only the front of the line ever gets served.
// A plain array can act as a queue: push adds to the back of the line,
// shift removes from the front — the person who's been waiting longest.
const checkoutLine = [];
checkoutLine.push("Priya"); // Priya joins the back of the line
checkoutLine.push("Sam"); // Sam joins behind Priya
checkoutLine.push("Devon"); // Devon joins behind Sam
// The cashier calls the next person — always from the FRONT.
const nextCustomer = checkoutLine.shift();
console.log(nextCustomer); // "Priya" — first in line, first served
console.log(checkoutLine); // ["Sam", "Devon"] — Priya is gone, order preserved
shift removes from the front, push adds to the back — different ends. This trips people up coming straight from stacks, where both operations touch the same end. A queue deliberately uses two different ends.shift on a real array is slower than pop, technically. For learning the concept, don't worry about this — it's a performance detail for later, not something that changes how a queue behaves.You just learned that a stack is an array used with rules at one end. A queue is the same array, with one small rule change: add at the back, remove from the front instead of the same end. Same starting material, one flipped rule, a completely different order of service.