First Bad Commit
Updated
Problem statement
The commits of a project are listed from oldest to newest. A test was run on every commit, and each one is labeled "good" (the test passed) or "bad" (the test failed). A bug got in at some commit and was never fixed, so once a commit is bad, every later commit is bad too.
Given the labels commits, return the 0-based index of the earliest bad commit. If every commit is good, return -1.
Try to read only O(log n) labels, where n is the number of commits.
Function
firstBadCommit(commits: String[]) → intExamples
Example 1
commits = ["good","good","good","bad","bad","bad"]return = 3Commits 0 to 2 are good and commit 3 is the first one marked bad.
Example 2
commits = ["good","good"]return = -1No commit is bad, so the answer is -1.
Example 3
commits = ["bad","bad","bad"]return = 0The very first commit is already bad.
Example 4
commits = ["good","bad"]return = 1Only the last commit is bad, so the answer is its index 1.
Constraints
1 <= commits.length <= 104- Each
commits[i]is"good"or"bad". - Every
"good"label comes before every"bad"label.
More WhatNot Phone Screen questions
- Adjacent Pair and Run RemovalPHONE SCREEN · Seen Nov 2026
- Merge Sorted Squares of Two ArraysPHONE SCREEN · Seen Nov 2026
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Knight Dialer SequencesPHONE SCREEN · Seen Oct 2026
- Reachable Exits in an Obstacle GridPHONE SCREEN · Seen Oct 2026
- Palindrome with One DeletionPHONE SCREEN · Seen Oct 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Attraction Route Without Reusing TrailsPHONE SCREEN · Seen Sep 2026
- Earliest Car for Each PassengerPHONE SCREEN · Seen Sep 2026
- Minimum Stores for a Shopping ListPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- First Element Greater Than KMeta · PHONE SCREENMeta interview questions
- Track New Values in Sorted OrderBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Price at Query TimestampsGoogle · PHONE SCREENGoogle interview questions
- First Missing Number in a Consecutive ArrayArista Networks · OA · PHONE SCREENArista Networks interview questions
- Messages in an Inclusive Timestamp RangeAttentive · PHONE SCREEN · ONSITE INTERVIEWAttentive interview questions
- Target Sum Across Two Sorted ArraysSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Leftmost Binary Search MatchByteDance · PHONE SCREENByteDance interview questions
- Count Occurrences in a Sorted ArrayAdobe · PHONE SCREEN · ONSITE INTERVIEWAdobe interview questions
- Find the First Target IndexSuperhuman · PHONE SCREENSuperhuman interview questions
- First Target IndexGrammarly · PHONE SCREEN