First Broken Build

EasyBinary Search≈ 15 min

A project's builds are numbered from 0, and their results are written in the string builds: P for a build that passed, F for one that failed. At some build a bug got in, and every build from that one on fails; every build before it passed.

Return the index of the first failed build, or -1 if every build passed.

Imagine each check is slow to run: look at as few builds as you can.

Examples

text
builds = "PPPFFF"   →  3
builds = "PPPP"     →  -1
builds = "FFF"      →  0
builds = "PF"       →  1

Constraints

  • Between 0 and 100,000 builds. Every P comes before every F.
  • Aim for O(log n): a history four times longer should take only a couple of checks more. Python's str.index and in, and TypeScript's indexOf, read from the start.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 4 visible cases here, in your browser. Submit runs all 8 on the judge, 4 of them hidden, and measures how your solution scales.