Find Earliest Compatible Release Version
Heaps, Sorting & SearchAuto-graded · 21 test casesLast 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:
- Semantic ordering is determined numerically by
(MAJOR, MINOR, PATCH). - Within any single
MAJORrelease line, if a version $v$ is compatible, then every version $v' \ge v$ belonging to that sameMAJORrelease line is also compatible. - Across different
MAJORrelease 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
versionsis unique and matches the pattern^[0-9]+\.[0-9]+\.[0-9]+$. compatible_versionsis a list of strings present inversions.
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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
-
Semantic Sorting: Lexicographical string comparison does not work for semantic versions (e.g.,
"1.10.0"is alphabetically before"1.2.0", but numerically greater). We parse each version string into an integer tuple(MAJOR, MINOR, PATCH)for correct ordering. -
Grouping by Major Version: Group all versions by their
MAJORcomponent. Sort theMAJORkeys in ascending order. -
Pruning & Binary Search (Branch Monotonicity):
- For each major branch (in increasing major order), sort its versions numerically.
- Test the last (largest) version of the branch. By monotonicity, if the largest version in a major branch is not compatible, then no version in that major branch can be compatible. We can prune the whole branch immediately.
- If the largest version is compatible, this branch contains the global minimum compatible version (since all versions in subsequent major branches are strictly larger).
- Perform binary search within this branch to locate the first compatible version.
Complexity
- Time Complexity: $\mathcal{O}(N \log N)$ where $N$ is the number of versions, primarily due to parsing and sorting the versions. The compatibility checks are minimized via binary search per branch.
- Space Complexity: $\mathcal{O}(N)$ to store parsed versions, major groups, and lookup set.
Python Reference Solution
from typing import List, Optional
def find_minimum_compatible_version(versions: List[str], compatible_versions: List[str]) -> Optional[str]:
compatible = set(compatible_versions)
branches = {}
for version in versions:
major, minor, patch = map(int, version.split('.'))
branches.setdefault(major, []).append(((major, minor, patch), version))
for major in sorted(branches):
candidates = sorted(branches[major])
# Check if the highest version in this major branch is compatible
if candidates[-1][1] not in compatible:
continue
# Binary search for the earliest compatible version in this branch
lo, hi = 0, len(candidates) - 1
while lo < hi:
mid = (lo + hi) // 2
if candidates[mid][1] in compatible:
hi = mid
else:
lo = mid + 1
return candidates[lo][1]
return None