Skip to the content

The first broken build

Problem

Every check costs a whole afternoon: install the build, play it through to the final boss and see whether the game freezes. The studio released its builds in order, and at some point a bug slipped in and never left: from that build on, every later one has it.

You get a list of booleans, one per build: false if it works and true if it is broken. All the good ones always come first and all the broken ones after. Return the position of the first broken build, counting from 0. With five builds where only the first two work, you return 2.

If none is broken, or the list is empty, return -1.

Don't check them one by one: look at the middle one and throw away the half where the first broken build cannot be, like in a dictionary.

Examples

  • Two good and three broken

    [false, false, true, true, true] → 2

  • Only the last one is broken

    [false, false, false, false, false, true] → 5

  • Two builds

    [false, true] → 1

  • Many builds

    [false, false, false, false, false, false, false, false, false, true, true, true, true, true, true, true] → 9

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def first_broken(builds):
    pass

JavaScript

function firstBroken(builds) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. It is free and you do not need an account to start.

More O(log n) challenges

See all challenges →