Reported September 2026
Vercelgraph

Order DAG Nodes from Leaves to Roots

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

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

The mistake that sinks a first attempt on this Vercel OA, reported in September 2026, is treating it like a plain topological sort and dumping nodes in whatever order the queue gives you. The problem wants layers. Each round, every current leaf comes out together, sorted ascending, then they get peeled off. It's a reverse Kahn's algorithm with a sorting step per layer. If the graph has a cycle, you return an empty array. If you blank on the layer logic mid-assessment, StealthCoder runs invisibly as a safety net and gives you a working solution.

The problem

You are given a directed graph whose edges point from parent nodes to child nodes. Repeatedly take every current leaf (a node with outdegree zero), emit that layer in ascending node order, and remove those nodes and their incoming edges.
Return the concatenation of the leaf layers. Return an empty array if a directed cycle prevents all nodes from being removed.

Function
leavesToRoots(n: int, edges: int[][]) → int[]

Examples
Example 1
n = 4
edges = [[0,1],[0,2],[2,3]]
return = [1,3,2,0]
Leaves 1 and 3 form the first sorted layer, followed by 2 and then 0.
Example 2
n = 3
edges = [[0,1],[1,2],[2,0]]
return = []
The cycle has no removable leaf.

Constraints
1 <= n <= 100000.
Every edge has two valid node IDs and appears once.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to flip Kahn's algorithm. Track outdegree for each node, and build a reverse adjacency list so each child knows its parents. Start with all nodes at outdegree zero. Sort that layer, append it to the result, then for each node in it decrement the outdegree of every parent. Any parent that hits zero joins the next layer. Repeat until no leaves remain. The common pitfall is using one global queue, which mixes layers and breaks the ascending order inside each one. Another is forgetting the cycle check. If the result length is less than n at the end, return an empty array, not a partial one. Complexity is O(n + e + n log n) since every node gets sorted once. With n up to 100000, avoid recursion. If the layer logic slips while you're live, StealthCoder is the hedge that covers you.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Order DAG Nodes from Leaves to Roots 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Vercel reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Order DAG Nodes from Leaves to Roots FAQ

What's the trick in the Vercel leaves-to-roots problem?+

Run Kahn's algorithm backwards. Count outdegrees, start with nodes at zero, and process in whole layers instead of one node at a time. Sort each layer before appending. When a node is removed, decrement the outdegree of its parents using a reverse adjacency list.

How do I detect the cycle case?+

Count how many nodes you emitted. If the total is less than n when no more leaves exist, a cycle blocked the rest, so return an empty array. Example 2 shows this: every node in the 3-cycle has outdegree one, so the first layer is empty.

Why can't I just use a single queue?+

A single queue mixes nodes from different layers and loses the ascending order within each layer. Example 1 needs [1,3] then [2] then [0]. A plain queue could emit them in a different order. Collect each layer, sort it, then move on to the next.

What's the time complexity and does it fit n = 100000?+

Building the graph is O(n + e). Each node is in exactly one layer, so total sorting is at most O(n log n). That fits comfortably at 100000. Use iterative loops, not recursion, to avoid stack depth issues.

How do I prepare for this in 48 hours?+

Write Kahn's algorithm from memory twice, once forward and once reversed with outdegrees. Then add the per-layer sort and the cycle check. Test on both examples and on a single isolated node. That covers the shape of this problem and its close variants.

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

OA at Vercel?
Invisible during screen share
Get it