Reported July 2026
Amazongraph

Resolve Task Dependencies

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

Amazon reported this one in July 2026, and the trap is hiding in the wording: "remove exactly one optional dependency" only applies when the full graph is cyclic. Resolve Task Dependencies looks like a plain topological sort, and it mostly is, a queue-based Kahn's algorithm with a twist. Candidates who skip the acyclic-first check or botch the tie-break lose points fast. You've got a short window before the assessment, so learn the shape now: detect the cycle, pick the cheapest fixing edge, then rerun the sort for the smallest order. If you blank mid-assessment, StealthCoder is the backup running invisibly on your screen.

The problem

There are n tasks numbered from 0 through n - 1. A dependency [u, v] means task u must be completed before task v.
The array mandatory contains dependencies that cannot be removed. Each row [u, v, cost] in optional contains a removable dependency u -> v and its cost.
If the graph containing every dependency is acyclic, return its lexicographically smallest topological order without removing anything.
If it is cyclic, consider removing exactly one optional dependency. Among the optional dependencies whose removal makes the entire graph acyclic, remove the one with the smallest cost. If costs tie, remove the one appearing earlier in optional. Return the lexicographically smallest topological order of the resulting graph.
If no single optional dependency can make the graph acyclic, return an empty array.

Function
resolveTaskOrder(n: int, mandatory: int[][], optional: int[][]) → int[]

Examples
Example 1
n = 4
mandatory = [[0,1],[2,3]]
optional = [[1,2,7]]
return = [0,1,2,3]
All dependencies are already acyclic, so none is removed. The only valid order is [0,1,2,3].
Example 2
n = 3
mandatory = [[0,1]]
optional = [[1,2,5],[2,0,2]]
return = [0,1,2]
The dependencies form the cycle 0 -> 1 -> 2 -> 0. Removing 2 -> 0 costs 2, which is cheaper than removing 1 -> 2.
Example 3
n = 2
mandatory = [[0,1],[1,0]]
optional = []
return = []
The mandatory dependencies form a cycle, and there is no optional dependency that can be removed.

Constraints
1 <= n <= 500
0 <= mandatory.length, optional.length
mandatory.length + optional.length <= 2000
Every dependency endpoint is in [0, n - 1], and no dependency is repeated.
0 <= cost <= 1000000000

Reported by candidates. Source: FastPrep

Pattern and pitfall

Run Kahn's algorithm with a min-heap instead of a plain queue. That gives the lexicographically smallest order. If all n nodes come out, return it and remove nothing. That's the edge case: don't touch an optional edge when the graph is already acyclic. If it's cyclic, try each optional edge one at a time. Rebuild the graph without it and test acyclicity. With n up to 500 and at most 2000 edges, that's roughly 2000 passes of O(E log n), which is fine. Among edges that work, pick the smallest cost, and break ties by the earliest index in optional. Then run the heap-based sort once more on that graph. Pitfalls: using a FIFO queue, which gives a valid order but not the smallest one, and removing an edge that doesn't break every cycle. If no single removal works, return an empty array. StealthCoder is the hedge if the tie-break logic slips under pressure.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Resolve Task Dependencies 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Resolve Task Dependencies FAQ

What's the trick in Resolve Task Dependencies?+

Use Kahn's algorithm with a min-heap so the order is lexicographically smallest. Check the full graph first. Only if it's cyclic do you try removing optional edges. Then pick the cheapest working edge, with ties going to the earlier index.

How hard is this Amazon OA question really?+

Medium. The topological sort is standard. The difficulty is the layered rules: acyclic shortcut, exactly one removal, cost then index tie-break, and the empty-array fallback. Miss one rule and hidden tests fail.

Do I need a min-heap or is a normal queue fine?+

You need a min-heap. A plain queue returns a valid topological order but not the lexicographically smallest. Always pop the smallest available node with indegree zero, then release its neighbors.

Is brute-forcing every optional edge too slow?+

No. Optional edges number at most 2000 and n is at most 500. Each check is a Kahn pass in O(V+E). Trying every edge is fast enough, so skip clever cycle-edge analysis unless you want it.

How do I prepare for this in 48 hours?+

Write Kahn's algorithm with a heap from memory. Then practice a wrapper that removes one edge, rebuilds indegrees, and checks that all n nodes were processed. Test your code on the three examples, especially the mandatory-cycle case that returns empty.

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