Network Layer Execution Order
Updated
Problem statement
Given the layers of a network and the data links between them, return an order to run the layers in which every layer runs after all the layers it reads from, or an empty list if no such order exists.
The network is built with two calls. addLayer(name) adds a layer, and layers lists the layer names in the order they were added. addLayerOutput(source, target) sends the output of layer source to layer target. Each such call is one outputs[i] = [source, target], and it means source must run before target.
A layer is ready when every layer it reads from has already run. Whenever several layers are ready at the same time, run the one that was added first. This makes the answer unique.
A layer with no links still runs, and it takes its place by the same rule. The same link may be given more than once; it still means only that source runs before target. If the links form a cycle, including a layer linked to itself, no valid order exists: return an empty list.
Function
executionOrder(layers: List<String>, outputs: List<List<String>>) → List<String>Examples
Example 1
layers = ["linear1","input","relu","linear2","add"]outputs = [["input","linear1"],["linear1","relu"],["relu","linear2"],["linear2","add"],["input","add"]]return = ["input","linear1","relu","linear2","add"]input is the only layer that reads from no other layer, so it runs first. linear1 was added before input, but it reads input's output, so it has to wait. After input, only linear1 is ready (add still waits for linear2), so the chain linear1, relu, linear2 follows, and add runs last.
Example 2
layers = ["head_b","head_a","embed","merge","norm"]outputs = [["embed","head_a"],["embed","head_b"],["head_a","merge"],["head_b","merge"]]return = ["embed","head_b","head_a","merge","norm"]At the start, embed and norm are both ready. embed was added earlier, so it runs first. Now head_b and head_a are ready too, and both were added before norm: head_b was added before head_a, so head_b runs, then head_a. Next merge is ready and was added before norm, so it runs, and norm, which has no links at all, runs last.
Example 3
layers = ["encoder","decoder","proj"]outputs = [["encoder","decoder"],["decoder","proj"],["proj","decoder"]]return = []decoder reads from proj, and proj reads from decoder, so neither can run before the other. No valid order exists, so the answer is an empty list, even though encoder on its own could run.
Constraints
1 <= layers.length <= 1000- Each layer name has
1to20characters, each a lowercase letter, a digit or_, and no name appears twice inlayers. 0 <= outputs.length <= 4000outputs[i].length == 2outputs[i][0]andoutputs[i][1]are both inlayers. They may be the same layer.
More NVIDIA Phone Screen questions
- Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
- Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
- Merge Multiple Sorted StreamsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Merge Event PreferencesAirbnb · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
- Course Schedule IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Task Scheduler with Prerequisites and Execution PathsBrex · PHONE SCREEN
- Dependency-Aware Task SchedulerRippling · PHONE SCREENRippling interview questions
- Lexicographically Smallest Topological OrderByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
- Merge Subsequences Into a Consistent Total OrderGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Lexicographic Dependency OrderIllumio · PHONE SCREENIllumio interview questions
- Topological Sort with Secondary OrderingRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Order Sources by Their DependenciesRetool · PHONE SCREENRetool interview questions
- Dependency-Aware Agent Task SchedulerOpenAI · PHONE SCREENOpenAI interview questions