Resolve package versions from dependency constraints
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in OpenAI's technical screen (coding), last asked May 2026. It tests parsing, graphs and backtracking.
Build the core of a package manager's resolver. Packages have versions, and each version may depend on other packages within a version range. Parse version constraints, answer which versions satisfy them, and finally choose exactly one version of every package you need so that every constraint holds at once, or report that no such choice exists.
Part 1: constraints
Unless you agree on a different interface with the interviewer, assume:
- Versions are
"MAJOR.MINOR.PATCH"and compare numerically (1.10.1is newer than1.2.0). - A constraint is a comma-separated list of clauses that must all hold, each
>=,>,<=,<or==followed by a version;*matches anything. satisfies(version: str, spec: str) -> bool-
matching(available: list[str], spec: str) -> list[str]— the satisfying versions, oldest first.
satisfies("1.4.2", ">=1.2.0,<2.0.0") -> True
satisfies("2.0.0", ">=1.2.0,<2.0.0") -> False
matching(["1.0.0", "1.2.0", "1.10.1", "2.0.0"], ">=1.2.0,<2.0.0") -> ["1.2.0", "1.10.1"]
Part 2: resolve
-
resolve(root: dict[str, str], registry: dict[str, dict[str, dict[str, str]]]) -> dict[str, str] | None—rootmaps the packages you ask for to constraints;registry[pkg][version]is that version's own dependencies. Return one version per package that is needed (directly or through dependencies) such that every constraint from every chosen version holds. Prefer the newest version that works. ReturnNonewhen impossible.
registry = {
"app": {"1.0.0": {"web": ">=2.0.0", "db": ">=1.0.0"}},
"web": {"2.0.0": {"json": ">=1.0.0,<2.0.0"}, "3.0.0": {"json": ">=2.0.0"}},
"db": {"1.0.0": {"json": "<2.0.0"}, "1.1.0": {"json": "<2.0.0"}},
"json": {"1.5.0": {}, "2.1.0": {}},
}
resolve({"app": "==1.0.0"}, registry)
-> {"app": "1.0.0", "web": "2.0.0", "db": "1.1.0", "json": "1.5.0"}
# web 3.0.0 needs json >= 2, but every db needs json < 2, so web falls back to 2.0.0
registry = {"a": {"1.0.0": {"c": ">=2.0.0"}}, "b": {"1.0.0": {"c": "<2.0.0"}},
"c": {"1.0.0": {}, "2.0.0": {}}}
resolve({"a": "*", "b": "*"}, registry) -> None
Constraints
- Up to a few thousand packages and tens of versions each.
- The general problem is NP-hard, so expect backtracking; make it fast on typical inputs.
Clarifying questions
- Which constraint operators are needed (
^,~,!=, pre-release tags)? - "Newest wins" for which package first: the root's, or every package greedily?
- On failure, return
Noneor an explanation of the conflict? - Can a dependency name a package that isn't in the registry?
- Are cycles between packages allowed?
What a strong answer covers
- A version type that compares numerically and a constraint parser built once, not reparsed per check.
- Backtracking search: pick an unresolved package, try its versions newest first, add its dependency constraints, check them against what's chosen, undo on conflict.
- Pruning: pick the package with the fewest remaining candidates; filter candidates as constraints arrive; remember conflicts so the same dead end isn't re-explored.
- Cost: exponential in the worst case; why this is SAT-like and how real resolvers (PubGrub, SAT solvers) learn from conflicts.
- Tests for diamonds, unsatisfiable pairs, and backtracking several levels deep.
Follow-ups
- Explain a failure to the user: "a needs c >= 2.0.0 but b needs c < 2.0.0".
- Minimize changes from an existing lockfile instead of always taking the newest.
- Allow two versions of the same package side by side, as some ecosystems do.
Where it's asked
- OpenAI · Technical screen: coding · Last asked May 202660 min · One problem in three to five growing parts filling the hour, usually in CoderPad; ML roles get a Colab notebook with autocomplete off · No AI assistants or autocomplete; documentation is fine
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
