LeetCode — First Bad Version

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

Example 1:

Input: n = 5, bad = 4
Output: 4
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version.

Example 2:

Input: n = 1, bad = 1
Output: 1


  • 1 <= bad <= n <= 231 - 1





Dell Technologies — VxRail Software Engineer 👨‍💻

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

8 Critical Elements to a Successful DevOps Transformation

Streaming Millions of Rows from Postgres to AWS S3

Outsourcing best practices: How to outsource R&D projects

Is there a public Pub/Sub Lite topic?

Duet Protocol Community Call on May 5th 2022

Clicks to conversions with BigQuery & Data Studio

what i have been waiting for has come !

Breaking the Project Management Triangle

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Steven Lu

Steven Lu

Dell Technologies — VxRail Software Engineer 👨‍💻

More from Medium

Implementation of Queue Data Structure using linked list — so easy🤔

Leetcode 8. String to Integer (atoi)

LeetCode — Odd Even Linked List

CSES — Counting Rooms Solution Explained(C++, Java, Python)