Push three values, pop one, and show what remains joined by dashes.
Acceptance criteria
- 1.#out should show 1-2.
DSA Tutorial · Stacks and Queues
These 10 tasks go with the Stacks 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: “A stack is an array where you agree only to touch the end. That restriction is the whole value — it makes the structure O(1) at everything it does, and it makes certain problems obvious.” .
1.Push and popexercise · easy · 10 min
Push three values, pop one, and show what remains joined by dashes.
Passes when: #out should show 1-2.
2.Peek the topexercise · easy · 10 min · practice plan
Show the value on top of the stack without removing it.
Passes when: #out should show top 3.
3.Balanced bracketsexercise · easy · 10 min · practice plan
Report whether the brackets are balanced.
Passes when: #out should show balanced.
4.Reverse with a stackexercise · easy · 10 min · practice plan
Reverse a string using a stack and show the result.
Passes when: #out should show cba.
5.Next greater elementexercise · medium · 10 min · practice plan
Use a monotonic stack to show the next greater value for the first element.
Passes when: #out should show next 5.
6.Fix shift used as popbug fix · easy · 8 min
shift removes from the bottom of the stack, not the top.
Passes when: #out should show 1-2.
7.Fix peek that removesbug fix · easy · 8 min · practice plan
pop takes the value off; peeking should leave it in place.
Passes when: #out should show still 3.
8.Fix the bracket check ignoring leftoversbug fix · medium · 8 min · practice plan
Unclosed brackets remain on the stack and must be checked at the end.
Passes when: #out should show unbalanced.
9.Fix the reverse reading from the frontbug fix · easy · 8 min · practice plan
Taking from the front rebuilds the original order.
Passes when: #out should show cba.
10.Fix the monotonic stack storing valuesbug fix · easy · 8 min · practice plan
Storing values instead of indices means the result cannot be written back.
Passes when: #out should show next 5.
Between them these tasks cover push and pop, Matching brackets and Monotonic stack.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.