Skip to content
RED SAGSkills

Resolve Dependency Version Ranges

A package manager must pick, for each requirement, the highest available version that satisfies its range. available maps package names to lists of versions (MAJOR.MINOR.PATCH, unsorted). requirements maps package names to range strings.

A range is one or more alternatives separated by ||; a version satisfies the range if it satisfies any alternative. An alternative is a space-separated list of comparators that must all hold (an empty alternative or * matches everything). Comparators:

- 1.2.3 or =1.2.3: exactly that version; >1.2.3, >=1.2.3, <1.2.3, <=1.2.3 compare numerically part by part - ~1.2.3: >=1.2.3 <1.3.0 - ^1.2.3: >=1.2.3 <2.0.0; if MAJOR is 0, ^0.2.3 means >=0.2.3 <0.3.0; if MAJOR and MINOR are 0, ^0.0.3 means >=0.0.3 <0.0.4

Return an object with one entry per requirement: the chosen version string, or null if none matches or the package is not available.

Example 1

Input: available = {"react":["17.0.2","18.2.0","18.3.1","19.0.0"],"lodash":["4.17.20","4.17.21"],"tiny":["0.2.3","0.2.9","0.3.0"],"zero":["0.0.3","0.0.4"]}, requirements = {"react":"^18.0.0","lodash":"~4.17.0","tiny":"^0.2.4","zero":"^0.0.3","missing":"*"}
Output: {"react":"18.3.1","lodash":"4.17.21","tiny":"0.2.9","zero":"0.0.3","missing":null}
Explanation: ^18.0.0 allows 18.x; ~4.17.0 allows 4.17.x; ^0.2.4 stays below 0.3.0.

Example 2

Input: available = {"a":["1.0.0","1.5.0","2.0.0","2.1.0"]}, requirements = {"a":">=1.2.0 <2.0.0"}
Output: {"a":"1.5.0"}

Constraints

  • Up to 200 packages with up to 200 versions each
  • No pre-release or build tags
Hint 1

Turn every range into a list of alternatives, each a list of [operator, version] pairs.

Hint 2

Compare versions as arrays of three numbers, never as strings.

Good practice for: Backend Developer, DevOps Engineer, Full Stack Developer, Frontend Developer, Cloud Engineer, Automation Test Engineer

Run checks your code on 3 sample tests. Submit runs all 10 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.