Repeatedly take the smallest and join the result.
Acceptance criteria
- 1.#out should show 1-3-5.
DSA Tutorial · Heaps and Priority Queues
These 10 tasks go with the Heap Sort and Streaming Data 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: “Heap sort is the sorting algorithm nobody uses and everybody should understand — it is the proof that O(n log n) is achievable without the extra memory merge sort needs.” .
1.Sort with repeated extractionexercise · easy · 10 min
Repeatedly take the smallest and join the result.
Passes when: #out should show 1-3-5.
2.Median of an odd listexercise · easy · 10 min · practice plan
Show the middle value of a sorted odd-length list.
Passes when: #out should show median 3.
3.Median of an even listexercise · easy · 10 min · practice plan
Show the average of the two middle values.
Passes when: #out should show median 3.
4.Two halves stay balancedexercise · easy · 10 min · practice plan
Report whether two halves differing by one are balanced.
Passes when: #out should show balanced.
5.Worst case guaranteeexercise · medium · 10 min · practice plan
Report heap sort's worst-case complexity class.
Passes when: #out should show n log n.
6.Fix the extraction taking the last elementbug fix · easy · 8 min
Removing from the end ignores which value is smallest.
Passes when: #out should show 1-3-5.
7.Fix the odd median indexbug fix · easy · 8 min · practice plan
Dividing the length by two overshoots the middle on odd lengths.
Passes when: #out should show median 5.
8.Fix the even median taking one valuebug fix · medium · 8 min · practice plan
With an even count the median is the average of the middle pair.
Passes when: #out should show median 3.
9.Fix the balance check allowing a gapbug fix · easy · 8 min · practice plan
Halves may differ by at most one element.
Passes when: #out should show lopsided.
10.Fix the claimed worst casebug fix · easy · 8 min · practice plan
Heap sort does not degrade on adversarial input the way quicksort can.
Passes when: #out should show n log n.
Between them these tasks cover In-place sorting, Running median and Worst-case bounds.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.