Push four values and show how many the array holds.
Acceptance criteria
- 1.#out should show four held.
DSA Tutorial · Big O and Complexity
These 10 tasks go with the Amortised Analysis 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: “Some operations are usually instant and occasionally expensive. Amortised analysis is how you describe that honestly instead of quoting either extreme.” .
1.Total pushesexercise · easy · 10 min
Push four values and show how many the array holds.
Passes when: #out should show four held.
2.Preallocate instead of growingexercise · easy · 10 min · practice plan
Create an array of a fixed size filled with zeros and show its length.
Passes when: #out should show sized six.
3.Doubling capacityexercise · easy · 10 min · practice plan
Starting at one, double until capacity reaches at least ten and report the result.
Passes when: #out should show capacity sixteen.
4.Amortised or averageexercise · easy · 10 min · practice plan
A guarantee over a sequence of operations, regardless of input — report amortised or average.
Passes when: #out should show amortised.
5.Pop is cheapexercise · medium · 10 min · practice plan
Remove the last element and show the remaining length as a word.
Passes when: #out should show two left.
6.Fix the growth by onebug fix · easy · 8 min
Adding one to capacity each time copies far more often than doubling.
Passes when: #out should show capacity sixteen.
7.Fix the fill that shares a referencebug fix · easy · 8 min · practice plan
fill with an array literal puts the same array in every slot.
Passes when: #out should show independent rows.
8.Fix shift used to remove the last itembug fix · easy · 8 min · practice plan
shift removes from the front and costs a full pass.
Passes when: #out should show two left.
9.Fix the sparse arraybug fix · medium · 8 min · practice plan
Assigning past the end leaves holes and the length jumps.
Passes when: #out should show sized six.
10.Fix the wrong guaranteebug fix · easy · 8 min · practice plan
Amortised holds for any input; average depends on the distribution.
Passes when: #out should show amortised.
Between them these tasks cover Amortised cost, Growth by doubling and Preallocation.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.