Interactive Problems
5 Interactive LeetCode problems · Browse tasks by difficulty and choose your next practice problem.
Learn the coding interview patterns behind these problems.
Treat each oracle call as part of the algorithm
Interactive problems expose an API instead of giving direct access to all data. The first step is to identify what one response proves. First Bad Version supplies a monotone predicate: once a version is bad, every later version is bad. That permits binary search; an arbitrary guessing oracle may not have the same property.
When to use it
Use binary search when the API partitions an ordered search range into two monotone regions. For mountain arrays or word-guessing tasks, design around their specific response contract and call budget rather than assuming ordinary array access is free.
Worked example
Versions 1 through 10; the first bad version is 7.
- Query 5: good. Keep the closed interval [6, 10].
- Query 8: bad. Keep [6, 8], since 8 could still be the first bad version.
- Query 7: bad, then query 6: good. The interval becomes [7, 7].
Return 7 after four API calls. Each update preserves the first bad version inside the interval.
Time and space
A monotone first-true search uses O(log n) oracle calls and O(1) auxiliary state. If oracle access is expensive or capped, track calls explicitly; caching repeated mountain-array accesses can trade O(log n) space for fewer repeated requests.
Mistakes to check
- Compute the midpoint as low + floor((high - low) / 2) in fixed-width arithmetic to avoid adding two large bounds.
- Use high = mid for a true answer when mid may be the first valid value. Moving past it discards a possible answer.
- Check whether an answer is guaranteed to exist. A search for a missing boundary needs a defined sentinel or a final validity check.
Further reading: LeetCode: First Bad Version API contract
Guide reviewed .