KatInterview

Find Earliest Compatible Release Version

Heaps, Sorting & SearchAuto-graded · 21 test cases
MediumAuto-gradedHeaps, Sorting & Searchbinary-searchsortingarrays

Last updated

Find the smallest semantic version that satisfies a monotonic compatibility check while minimizing evaluations.

You are given a list of semantic version strings versions, where each string has the format "MAJOR.MINOR.PATCH" (with non-negative integer components).

You are also provided a list compatible_versions containing all version strings that are compatible. (In a real system, checking compatibility is an expensive operation, so your algorithm should query compatibility efficiently).

Version compatibility follows a major-branch monotonicity rule:

  1. Semantic ordering is determined numerically by (MAJOR, MINOR, PATCH).
  2. Within any single MAJOR release line, if a version $v$ is compatible, then every version $v' \ge v$ belonging to that same MAJOR release line is also compatible.
  3. Across different MAJOR release lines, compatibility is independent.

Your task is to find and return the lexicographically earliest (numerically smallest) version from versions that is compatible. If no version in versions is compatible, return None (or null in Python terms).

Constraints

  • $1 \le \text{len}(versions) \le 10^5$
  • Each element of versions is unique and matches the pattern ^[0-9]+\.[0-9]+\.[0-9]+$.
  • compatible_versions is a list of strings present in versions.

Monotonicity & Query Efficiency Note

Your logic should exploit branch monotonicity: checking the latest version of a MAJOR branch instantly reveals whether that branch contains any compatible versions. If it does, a binary search can locate the earliest compatible version in that branch.

Examples

Example 1

Input: find_minimum_compatible_version(['1.0.0', '1.1.0', '1.2.0', '2.0.0', '2.1.0'], ['1.2.0', '2.0.0', '2.1.0'])

Output: '1.2.0'

Example 2

Input: find_minimum_compatible_version(['1.0.0', '1.0.5', '2.0.0', '2.1.0'], ['2.1.0'])

Output: '2.1.0'

Example 3

Input: find_minimum_compatible_version(['0.0.0', '0.0.1', '1.0.0'], [])

Output: None

Hints

Hint 1

Convert version strings to numerical tuples (MAJOR, MINOR, PATCH) so they sort correctly (e.g., '1.10.0' comes after '1.2.0').

Hint 2

Group versions by their MAJOR release line.

Hint 3

Because monotonicity holds per MAJOR branch, if the highest version in a major branch is incompatible, the entire branch is incompatible. You can skip it in O(1) checks.

Hint 4

Since versions in a smaller major branch are strictly smaller than those in a larger major branch, process major branches in ascending numerical order. The first branch with a compatible version holds the global minimum.