Find Minimum Compatible Version
Company: OpenAI
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates binary search, monotonic predicates, careful version ordering, and cost-aware optimization of expensive compatibility checks. It is commonly asked in the Coding & Algorithms domain to test reasoning about monotonicity, minimizing API calls, and adapting search strategies when monotonicity holds only within subranges; the level of abstraction combines conceptual understanding of monotone properties with practical application of efficient search techniques.
Part 1: First Compatible Version
Constraints
- `0 <= len(versions) <= 200000`
- `len(versions) == len(compatible)`
- `versions` is strictly increasing
- `compatible` has the form `False...False, True...True`
Examples
Input: ([100, 101, 102, 103, 104], [False, False, True, True, True])
Expected Output: 102
Explanation: The first compatible entry is at index 2, so the answer is version 102.
Input: ([5, 7, 9], [False, False, False])
Expected Output: None
Explanation: No version is compatible.
Input: ([2, 4, 8], [True, True, True])
Expected Output: 2
Explanation: All versions are compatible, so return the first one.
Input: ([42], [False])
Expected Output: None
Explanation: Single-element edge case with no compatible version.
Input: ([], [])
Expected Output: None
Explanation: Empty input has no answer.
Hints
- This is the same as finding the leftmost `True` in a monotone boolean array.
- If `mid` is compatible, keep searching to the left because there may be an earlier working version.
Part 2: First Compatible Index Under a Call Budget
Constraints
- `0 <= len(compatible) <= 1000000`
- `compatible` is monotone nondecreasing
- Your algorithm should run in `O(log n)` time
Examples
Input: ([False, False, True, True, True],)
Expected Output: 2
Explanation: The first compatible index is 2.
Input: ([False, False, False],)
Expected Output: -1
Explanation: There is no `True` value.
Input: ([True, True, True],)
Expected Output: 0
Explanation: If the first element is already compatible, return index 0.
Input: ([False],)
Expected Output: -1
Explanation: Single-element edge case with no compatible version.
Input: ([],)
Expected Output: -1
Explanation: Empty input has no valid index.
Hints
- You only need the leftmost `True` position, not the whole boundary.
- Using a half-open search range `[lo, hi)` makes the final boundary handling cleaner.
Part 3: Minimum Compatible Semantic Version with Layered Monotonicity
Constraints
- `0 <= len(versions) <= 200000`
- `len(versions) == len(compatible)`
- `versions` is sorted lexicographically and contains distinct tuples
- Patch-level monotonicity holds inside each fixed `(major, minor)` block
- Minor-representative monotonicity holds inside each `major`
- Major-representative monotonicity holds across majors
Examples
Input: ([(1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1), (2, 0, 0), (2, 0, 1), (2, 1, 0), (2, 1, 1), (3, 0, 0), (3, 0, 1)], [False, True, False, True, False, False, False, True, True, True])
Expected Output: (1, 0, 1)
Explanation: The overall sequence is not globally monotone, but the layered representative checks still lead to major 1, minor 0, patch 1.
Input: ([(1, 0, 0), (1, 0, 1), (2, 0, 0), (2, 0, 1), (2, 1, 0), (2, 1, 1), (3, 0, 0), (3, 0, 1)], [False, False, False, True, True, True, True, True])
Expected Output: (2, 0, 1)
Explanation: Major 1 has no compatible version, so the minimum compatible version is found in major 2.
Input: ([(1, 0, 0), (1, 0, 1), (2, 0, 0)], [False, False, False])
Expected Output: None
Explanation: No version is compatible in any group.
Input: ([(4, 2, 9)], [True])
Expected Output: (4, 2, 9)
Explanation: Single-version edge case.
Input: ([], [])
Expected Output: None
Explanation: Empty input has no answer.
Hints
- The last patch of a minor tells you whether that minor contains any compatible patch at all.
- Do three searches: first find the earliest compatible major, then the earliest compatible minor inside it, then the earliest compatible patch inside that minor.