You have multiple versions of a program, each numbered sequentially (starting from 0). Write a function that finds the first bad version given an isBad(version)
predicate.
Key Constraints
- All versions after the first bad version are also bad.
- Input versions are non-negative integers.
- If no bad version exists, return
-1
.
- Use binary search for optimal
O(log n)
performance.
Signature
function firstBadVersion(isBad: (version: number) => boolean): (n: number) => number
The function returns another function that, given the total number of versions n
, returns the first bad version index (or -1
).
Example
const isBad = (v) => v >= 4; // version 4 and later are bad
const findFirst = firstBadVersion(isBad);
findFirst(10); // returns 4
findFirst(3); // returns -1 (no bad versions)