Skip to the code
cf.
+10 XP

#151 First Failing Commit

easy

A visual regression test started failing somewhere in your branch. You ran it on every commit, oldest first, and stored the results in results: false means the test passed and true means it failed.

Once a commit breaks the test, every later commit fails too, so results is some false values followed by some true values.

Write firstFailing(results) that returns the index of the first true, or -1 if every commit passed. Look at as few results as you can.

Example 1

Input: results = [false,false,false,true,true]
Output: 3

Commit 3 is the first one where the test fails.

Example 2

Input: results = [false,false]
Output: -1

Every commit passed.

Constraints

  • 1 <= results.length <= 100000
  • No false comes after a true.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).