Push a value onto the end and show the array joined by dashes.
Acceptance criteria
- 1.#out should show 1-2-3.
DSA Tutorial · Arrays
These 10 tasks go with the Arrays in JavaScript for DSA 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: “Almost every DSA problem starts with an array. Knowing which of its methods are cheap and which quietly cost O(n) is most of what separates a fast solution from a slow one.” .
1.Append cheaplyexercise · easy · 10 min
Push a value onto the end and show the array joined by dashes.
Passes when: #out should show 1-2-3.
2.Remove from the endexercise · easy · 10 min · practice plan
Pop the last value and show what remains, joined by dashes.
Passes when: #out should show 4-5.
3.Insert in the middleexercise · easy · 10 min · practice plan
Use splice to insert 9 between the two values and show the result.
Passes when: #out should show 1-9-2.
4.Build without copyingexercise · easy · 10 min · practice plan
Collect three doubled values into an array using push, then join them.
Passes when: #out should show 2-4-6.
5.Preallocate a rowexercise · medium · 10 min · practice plan
Create an array of four zeros and show it joined by dashes.
Passes when: #out should show 0-0-0-0.
6.Fix unshift used to appendbug fix · easy · 8 min
unshift adds to the front and shifts everything along.
Passes when: #out should show 1-2-3.
7.Fix shift used to drop the last valuebug fix · easy · 8 min · practice plan
shift removes the first element, not the last.
Passes when: #out should show 4-5.
8.Fix splice deleting instead of insertingbug fix · easy · 8 min · practice plan
The delete count is one, so it replaces rather than inserts.
Passes when: #out should show 1-9-2.
9.Fix the copy inside the loopbug fix · easy · 8 min · practice plan
Rebuilding the array every pass is quadratic and loses the values here.
Passes when: #out should show 2-4-6.
10.Fix the array with a holebug fix · medium · 8 min · practice plan
Assigning past the end leaves an empty slot that joins as nothing.
Passes when: #out should show 0-0-0-0.
Between them these tasks cover Operation costs, push vs unshift and Avoiding copies.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.