← Questions

Build, validate and prune a computation graph

0
NvidiaCodingLast asked Feb 2026

A coding interview question asked in Nvidia's coding round, last asked Feb 2026. It tests graphs, cycle detection and dfs.

Implement a small graph of neural-network operations. Nodes are operations (an input, a weight, a matrix multiply, an activation, an output) and edges say which node feeds which. Build the graph, check that it is well formed, detect cycles, and remove every node that does not contribute to the outputs you need.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • add_node(node_id: str, op: str) -> None adds a node. op is one of input, const, matmul, add, relu, output, print. Adding an existing ID is an error.
  • add_edge(src: str, dst: str) -> None makes src an input of dst. The order of calls gives the order of a node's inputs.
  • validate() -> list[str] returns every rule broken, as readable messages, or []. Rules: every edge points at existing nodes; each op has the right number of inputs (input and const 0, relu, output and print 1, matmul and add 2); no cycles.
  • find_cycle() -> list[str] | None returns the nodes of one cycle in order, or None.
  • prune(required: list[str]) -> list[str] removes every node that no required node depends on, directly or indirectly, and returns the removed IDs sorted.

Example

add_node("x", "input");  add_node("w", "const")
add_node("mm", "matmul"); add_node("act", "relu")
add_node("dbg", "print"); add_node("y", "output")
add_edge("x", "mm"); add_edge("w", "mm")
add_edge("mm", "act"); add_edge("act", "y")
add_edge("mm", "dbg")
validate()        -> []
prune(["y"])      -> ["dbg"]      # nothing required depends on dbg

add_edge("act", "mm")
find_cycle()      -> ["mm", "act"]   # or the same cycle from another start
validate()        -> ["mm (matmul) has 3 inputs, expects 2", "cycle: mm -> act -> mm"]

Constraints

  • Up to 10^5 nodes and edges; prune and validate should run in O(V + E).
  • Node IDs are unique strings.

Clarifying questions

  • Should validation stop at the first error or report all of them?
  • Can a node feed the same consumer twice (for example add(x, x))?
  • When pruning, are input nodes that nothing needs also removed, or kept because they are part of the model's signature?
  • Should prune fail if the graph has a cycle?

What a strong answer covers

  • Adjacency lists in both directions (inputs and consumers), so arity checks and pruning are cheap.
  • Cycle detection with a three-color DFS (iterative, to avoid recursion limits) or Kahn's algorithm, and recovering the cycle's nodes from the DFS stack.
  • Pruning as a reverse traversal from the required nodes: everything reached is live, the rest is removed along with its edges.
  • Clear error reporting, and keeping the graph unchanged when an operation is rejected.
  • Tests: an empty graph, a self-loop, a node with no consumers, a required node that does not exist.

Follow-ups

  • Return a topological order for execution, and say how you would schedule independent nodes in parallel.
  • Add shape inference and report shape mismatches in validate.
  • Fold constants: a subgraph that depends only on const nodes is replaced by its value.

Where it's asked

  • Nvidia · Coding · Last asked Feb 2026Live coding, often C++-leaning; each round usually opens with 10 to 15 minutes on your past work · Assume no AI unless the interviewer offers it

What it tests

graphs · cycle detection · dfs

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding