Reported July 2026
Amazongraph

Package Dependency Order

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

The Amazon OA reported in July 2026 hinges on one data structure: an adjacency list built from the dependency pairs. Package A needs B and C, B needs D, E, F, and so on. You install the target and everything it transitively needs, with dependencies first. That's a topological order restricted to the target's reachable subgraph, and a cycle means you return an empty array. It looks like a graph problem dressed up as package management, and it is. If you blank on the cycle check during the live assessment, StealthCoder runs invisibly as a safety net and gives you the working solution.

The problem

You are given package dependency pairs and a target package. Each pair [package, dependency] means the package depends on that dependency.
Return an order in which to install the target package and all of its transitive dependencies so that every dependency appears before the package that needs it.
If there is a cyclic dependency among packages needed by the target package, return an empty array.
The source noted that multiple valid orders may exist. FastPrep uses this deterministic rule: process dependencies in the order they appear in the input pairs.

Function
packageInstallOrder(dependencies: String[][], target: String) → String[]

Examples
Example 1
dependencies = [["A","B"],["A","C"],["B","D"],["B","E"],["B","F"],["C","F"],["F","G"],["H","I"],["H","J"],["J","G"]]
target = "A"
return = ["D","E","G","F","B","C","A"]
The order installs D, E, and F before B; installs G before F; installs F before C; and installs both B and C before A.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Build a map from each package to its list of dependencies, keeping the input pair order. Then run a DFS post-order from the target. Use three states per node: unvisited, visiting, done. When you enter a node, mark it visiting. Recurse into each dependency in input order. If you hit a node that's already visiting, that's a cycle, so return an empty array. When all dependencies finish, mark the node done and append it to the result. Post-order gives you dependencies before dependents, which matches the expected output D, E, G, F, B, C, A. The common pitfall is a plain visited set. It can't tell a cycle from a shared dependency like F, which both B and C need. Another pitfall is walking packages unrelated to the target, like H, I, J. Only start from the target. StealthCoder is your hedge if the three-state DFS slips your mind mid-assessment.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Package Dependency Order cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Package Dependency Order FAQ

What's the trick in the Amazon Package Dependency Order problem?+

Run a DFS from the target and append each package after all its dependencies finish. That post-order is your install order. Track a visiting state separately from done so you can detect cycles. Iterate dependencies in the order they appear in the input pairs to match the deterministic rule.

How do I detect a cyclic dependency here?+

Use three states: unvisited, in-progress, finished. If DFS reaches a node that's in-progress, you've looped back on the current path, so return an empty array. A node that's finished is just a shared dependency, and you skip it. Only packages reachable from the target matter.

Do I need Kahn's algorithm or is DFS enough?+

DFS post-order is simpler and fits the expected output order directly. Kahn's algorithm works too, but you'd need to restrict to the reachable subgraph and the output order can differ from the example. For the deterministic rule given, DFS is the safer choice.

Why is the expected output D, E, G, F, B, C, A?+

From A, DFS goes to B first, then B's dependencies D, E, F in order. F needs G, so G is added before F. After B finishes, it goes to C, whose dependency F is already done. Then A is added last. Post-order produces exactly that sequence.

How do I prepare for this in 48 hours?+

Write the DFS with cycle states from scratch twice. Practice building an adjacency list from pairs, handling a target with no dependencies, and returning an empty array on a cycle. Test with shared dependencies and unrelated packages. That covers nearly every edge case this problem can throw.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it