Queues
JavaScript · Unit 23: Data structures
- Full plan
- JavaScript
- 10 exercises
A queue is the line at the bakery: whoever arrives stands at the back and the person at the front gets served. The first one in is the first one out.
An array is enough again: push joins the back and shift serves the front.
const line = [];
line.push("mia");
line.push("leo");
console.log(line.shift());
console.log(line);Prints
mia [ 'leo' ]
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
2. Complete the code
Fill in the blank to serve whoever is next in line.
3. Predict the output
What does this code print?
4. Find the bug
It should serve the line in the order they arrived: mia, leo and ava. Which line has the error?
5. Complete the code
Fill in the blank so leo joins the back of the line.
6. Multiple choice
A printer keeps the documents people send it and prints them one by one, in the order they arrived. Which structure fits?
7. Put the lines in order
Put
rotate(q, k)in order. It sends the firstkpeople to the back of the line and returns the line.8. Predict the output
Both arrays start out the same. What does it print?
9. Find the case that fails
nextUp(n)servesnpeople from a line of 3 and returns who's next, or "nobody" if there's no one left. Which call breaks it?10. Predict the output
There's a regular line and a priority one, and the priority one is served first. What does it print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.