Skip to main content

First Bad Version

Software Deployment Bug Tracker You're managing software releases. A bug was introduced in version X, causing all versions from X onward to fail. Given n total versions, find the first bad version using the minimum number of API calls. Example: n = 5, bad = 4 → 4 (versions 4 and 5 are bad)

Binary search is the tool here, but the real test is whether your invariant stays crisp. It's a great warm-up that builds boundary precision without a ton of moving parts. Interviewers care more about airtight boundaries than speed here.

Examples

You write

first_bad_version(n, bad)

  1. Example 1

    Input
    n = 5, bad = 4
    returns
    4
  2. Example 2

    Input
    n = 1, bad = 1
    returns
    1

The pattern behind it

Binary Search

I reach for it when

  • The input is sorted, or sorted and then rotated.
  • You want a boundary: the first or last place something is true.

Say this before you type. Say the yes-or-no question, and which side each answer throws away.

All 19 Binary Search problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Find First Bad Versioneasy