Add three values, remove the first, and join what remains.
Acceptance criteria
- 1.#out should show 2-3.
DSA Tutorial · Stacks and Queues
These 10 tasks go with the Queues in JavaScript chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “JavaScript has no queue. The obvious implementation with shift() is quadratic, and it is the most common accidental performance bug in this whole subject.” .
1.Enqueue and dequeueexercise · easy · 10 min
Add three values, remove the first, and join what remains.
Passes when: #out should show 2-3.
2.Peek the frontexercise · easy · 10 min · practice plan
Show the value at the front without removing it.
Passes when: #out should show front 1.
3.Dequeue with a pointerexercise · easy · 10 min · practice plan
Use an index instead of shift and show the value removed.
Passes when: #out should show took 1.
4.Queue size with a pointerexercise · easy · 10 min · practice plan
Report how many items remain after two pointer dequeues.
Passes when: #out should show one left.
5.Level order walkexercise · medium · 10 min · practice plan
Walk a small tree breadth first and join the values seen.
Passes when: #out should show 1-2-3.
6.Fix pop used as dequeuebug fix · easy · 8 min
pop removes from the back, so the queue behaves like a stack.
Passes when: #out should show 2-3.
7.Fix the peek reading the backbug fix · easy · 8 min · practice plan
The front of a queue is index zero, not the last slot.
Passes when: #out should show front 1.
8.Fix the pointer never advancingbug fix · easy · 8 min · practice plan
Without moving head, the same item is dequeued forever.
Passes when: #out should show took 2.
9.Fix the size counted from lengthbug fix · easy · 8 min · practice plan
With a pointer queue, length still counts the items already removed.
Passes when: #out should show one left.
10.Fix the breadth-first walk using a stackbug fix · medium · 8 min · practice plan
Taking from the back turns the level order into a depth-first walk.
Passes when: #out should show 1-2-3.
Between them these tasks cover Enqueue and dequeue, Pointer queues and BFS order.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.