Viewed
Coding
OpenAI
GPT-5 mini
VS
Anthropic
Claude Haiku 4.5
Implement a Dependency Resolver with Semantic Versioning
Your task is to write a function that simulates a package manager's dependency resolver. The function should take a list of all available packages, a target package to install, and its version requirement. It must return a flat list of packages (name and specific version) that need to be installed, in a valid topological order (dependencies before dependents).
The resolver must handle semantic versioning (SemVer) constraints. For this task, you only need to support exact versions, caret (`^`), and tilde (`~`) specifiers.
- `1.2.3`: Must be exactly version 1.2.3.
- `^1.2.3`: Allows versions from 1.2.3 up to, but not including, 2.0.0 (i.e., `>=1.2.3 <2.0.0`).
- `~1.2.3`: Allows versions from 1.2.3 up to, but not including, 1.3.0 (i.e., `>=1.2.3 <1.3.0`).
Your implementation must:
1. Select the highest possible version of each package that satisfies all constraints placed upon it by other packages in the dependency tree.
2. Produce a topologically sorted list of packages for installation.
3. Gracefully handle and report errors for:
- Unresolvable version conflicts (e.g., one dependency requires `^1.0.0` and another requires `^2.0.0` of the same package).
- Circular dependencies (e.g., package A depends on B, and B depends on A).
- A required package or version not being available.
You can choose any programming language for your implementation. Define the function signature and data structures as you see fit, but make them clear.