Find the element at sorted index two and show it.
Acceptance criteria
- 1.#out should show value 7.
DSA Tutorial · Sorting
These 10 tasks go with the QuickSelect: the Kth Element Without Sorting 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: “Sorting to find one element does far more work than the question asked for. QuickSelect answers it in linear time on average by discarding half the data at every step.” .
1.Third smallestexercise · easy · 10 min
Find the element at sorted index two and show it.
Passes when: #out should show value 7.
2.Smallest overallexercise · easy · 10 min · practice plan
Show the smallest value using sorted index zero.
Passes when: #out should show value 3.
3.Largest overallexercise · easy · 10 min · practice plan
Show the largest value by picking the last sorted index.
Passes when: #out should show value 20.
4.Partition stepexercise · easy · 10 min · practice plan
Partition around the last element and show where the pivot lands.
Passes when: #out should show pivot at 1.
5.Leave the input aloneexercise · medium · 10 min · practice plan
Copy before selecting and confirm the source is unchanged.
Passes when: #out should show source intact.
6.Fix the k treated as a countbug fix · easy · 8 min
Sorted index two is the third smallest, not the second.
Passes when: #out should show value 7.
7.Fix the largest read from the frontbug fix · easy · 8 min · practice plan
After an ascending sort the largest sits at the end.
Passes when: #out should show value 20.
8.Fix the partition counting the pivotbug fix · medium · 8 min · practice plan
Using <= lets a value equal to the pivot cross the boundary.
Passes when: #out should show pivot at 1.
9.Fix the select that reorders the caller's arraybug fix · easy · 8 min · practice plan
Sorting without copying rearranges the original.
Passes when: #out should show source intact.
10.Fix the fixed pivotbug fix · easy · 8 min · practice plan
Always taking the first element degrades to a quadratic scan on sorted input.
Passes when: #out should show random pivot.
Between them these tasks cover Partitioning, Discarding a half and Kth element.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.