Add binary-search module: iterative/recursive search, overflow-safe midpoint, lowerBound/upperBound, rotated arrays, search on the answer, 10 interview problems (14 tests, 14 transcripts)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b3tGdV3Vm51QUZxg2x
This commit is contained in:
Claude
2026-11-22 19:47:36 +05:30
parent 3336e2ba15
commit ce12a80f58
39 changed files with 2110 additions and 0 deletions
@@ -0,0 +1,20 @@
Binary search on the answer: how many times the yes/no question was asked
=========================================================================
Integer square root = largest m with m*m <= x; the question is "is m*m > x?"
sqrt(8) = 2 asked 3 times, instead of trying up to 9 candidates
sqrt(16) = 4 asked 4 times, instead of trying up to 17 candidates
sqrt(1,000,000,007) = 31622 asked 30 times, instead of trying up to 1,000,000,008 candidates
sqrt(2,147,483,647) = 46340 asked 31 times, instead of trying up to 2,147,483,648 candidates
Koko eating bananas: smallest speed k with sum(ceil(pile / k)) <= h
piles=[3, 6, 7, 11] h=8 -> speed 4 asked 4 times (candidates 1..11)
piles=[30, 11, 23, 4, 20] h=5 -> speed 30 asked 4 times (candidates 1..30)
piles=[30, 11, 23, 4, 20] h=6 -> speed 23 asked 5 times (candidates 1..30)
for [3, 6, 7, 11], hours needed at speed 1..11: 27, 15, 10, 8, 8, 6, 5, 5, 5, 5, 4
is that <= 8? F F F T T T T T T T T
piles=[1000000000, 999999999, 500000000] h=10 -> speed 250,000,000 asked 29 times (candidates 1..1,000,000,000); checked: speed-1 needs 13 hours
Ship packages within D days: smallest capacity so that, loading in order, D days are enough
weights=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] days=5 -> capacity 15 asked 5 times (candidates 10..55)
days needed at capacity 15: 5; at capacity 14: 6