KatInterview

First Flagged Build in a Deployment Pipeline

Heaps, Sorting & SearchAuto-graded · 18 test cases
MediumAuto-gradedHeaps, Sorting & Searchbinary-searcharraysmonotonic-predicate

Last updated

Tests whether a candidate can recognize a monotonic predicate and apply binary search to find the first transition point without comparing version strings.

Problem

Your team runs an automated deployment pipeline that releases software builds in chronological order. Each build has a version label such as "2.3.1" or "10.0.4" following the MAJOR.MINOR.PATCH format.

An external health-check system has already evaluated every build and recorded the results in a boolean list called flagged. A True entry means that build introduced a critical regression; a False entry means it passed. The pipeline guarantees a clean-then-broken structure: once a build is flagged, every later build is also flagged.

Given the ordered list of version labels and the corresponding flag results, return the version label of the first flagged build. If no build is flagged, return an empty string "".

Constraints

  • 1 <= len(versions) == len(flagged) <= 10^5
  • Each element of versions is a non-empty string in MAJOR.MINOR.PATCH format where each component is a non-negative integer.
  • flagged is a list of booleans that forms a (possibly empty) prefix of False values followed by a (possibly empty) suffix of True values.
  • The two lists have the same length.
  • Minimize the number of entries in flagged you inspect.

Examples

Example 1

Input: first_flagged_build(['1.0.0', '1.0.1', '1.1.0', '1.2.0'], [False, False, True, True])

Output: '1.1.0'

Example 2

Input: first_flagged_build(['1.0.0', '1.0.1'], [False, False])

Output: ''

Hints

Hint 1

The version strings are not what you search over — you are searching over indices. Ask yourself: what property of the index array makes binary search applicable?

Hint 2

Think about a half-open window [lo, hi). What invariant can you maintain about what is known to be flagged versus not flagged at every step?

Hint 3

When you probe the midpoint and it is flagged, the answer could be at mid or earlier — shrink from the right. When it is not flagged, the answer must be strictly to the right — shrink from the left. When lo == hi you are done.