Find the smallest eating speed that finishes in eight hours.
Acceptance criteria
- 1.#out should show speed 4.
DSA Tutorial · Searching
These 10 tasks go with the Binary Search on the Answer 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: “Binary search is not really about arrays. It is about any question where, once something becomes true, it stays true — and that covers far more problems than sorted lists.” .
1.Smallest speedexercise · easy · 10 min
Find the smallest eating speed that finishes in eight hours.
Passes when: #out should show speed 4.
2.Integer square rootexercise · easy · 10 min · practice plan
Find the largest whole number whose square is at most 17.
Passes when: #out should show root 4.
3.First trueexercise · easy · 10 min · practice plan
Find the first index where the predicate becomes true.
Passes when: #out should show first 3.
4.Check monotonicityexercise · easy · 10 min · practice plan
Report whether a predicate list is false then true with no flip back.
Passes when: #out should show monotonic.
5.Count the checksexercise · medium · 10 min · practice plan
Count how many probes a search over sixteen candidates needs.
Passes when: #out should show four probes.
6.Fix the low bound that never advancesbug fix · easy · 8 min
Setting low to mid rather than mid plus one stalls, so a guard stops it.
Passes when: #out should show speed 4.
7.Fix the midpoint bias when maximisingbug fix · medium · 8 min · practice plan
Searching for a maximum needs the midpoint rounded up, or it stalls.
Passes when: #out should show root 4.
8.Fix the search over data instead of answersbug fix · easy · 8 min · practice plan
The answer range is not the array indices here.
Passes when: #out should show speed 4.
9.Fix the first-true search returning the lastbug fix · easy · 8 min · practice plan
Moving low past a true value walks off the boundary.
Passes when: #out should show first 3.
10.Fix the non-monotonic predicatebug fix · easy · 8 min · practice plan
A predicate that flips back cannot be binary searched.
Passes when: #out should show flips back.
Between them these tasks cover Monotonic predicates, Answer ranges and Boundary search.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.