C++ Layer Graph API: Code Review, Building an FFN Block, and Layer Scheduling
Company: NVIDIA
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Technical Screen
This problem centers on a C++ class, `Network`, that describes a neural network as a graph of layers through two methods:
- `addLayer` adds a layer to the network.
- `addLayerOutput` declares a data dependency between two layers: the output of one layer becomes an input of another.
The exact signatures were not reported. For concreteness, assume the interface below and adapt it if your interviewer's version differs:
```cpp
struct Layer; // base class for Linear, activation, Add, ...
class Network {
public:
int addLayer(std::unique_ptr<Layer> layer); // adds a layer and returns its id
void addLayerOutput(int from, int to); // the output of `from` is an input of `to`
};
```
The round had three steps, worked in order in about 35 minutes.
### Clarifying Questions
- Does `addLayer` take ownership of the layer, or does the caller keep it alive?
- How are the network's input and output represented: as special layers, or implicitly as layers with no producers or no consumers?
- Can a layer have several inputs whose order matters, such as a concatenation, and how is that order specified?
### Part 1 — Review the C++ implementation
You are first shown a C++ implementation of `Network` and asked to review its code quality. That code was not reported, so describe what you would check in a class like this, which defects you would expect to find, and how you would fix each one.
```hint Lifetimes and invalid calls
Ask who owns each layer object, what keeps a stored reference to a layer valid as more layers are added, and which arguments to `addLayerOutput` could leave the graph in a bad state.
```
#### What This Part Should Cover
- Ownership and lifetime of layers, and the validity of stored references to them
- Validation of the arguments to both methods and the graph invariants they protect
- Interface quality: const-correctness, encapsulation, error reporting, naming
- Ranking correctness and memory-safety defects above style, with a concrete fix for each
### Part 2 — Build an FFN module with the API
You are then given an example feed-forward network (FFN) module and asked to build it with the `Network` API. Assume the example is the common transformer feed-forward block: a linear projection that expands the hidden size, a nonlinear activation, and a linear projection back to the hidden size. Write the calls that build it, and show how a residual connection would be expressed if the example included one.
```hint Edges carry data
Draw the module's diagram first, including where its input enters; each arrow in the diagram becomes one `addLayerOutput` call.
```
#### Clarifying Questions for this Part
- Which activation does the example use, and does it include a residual connection, normalization or dropout?
- Should the construction code be reusable, so that several blocks can be stacked?
#### What This Part Should Cover
- One layer per operation and one edge per data flow, in the correct direction
- How the block's input and output are represented and connected
- Branching and merging, such as a residual addition with two inputs
- Readable construction code, for example a helper that returns the block's output layer
### Part 3 — Compute a valid execution order
Implement a method that returns an order in which the layers can be executed, so that every layer runs after all the layers whose outputs it consumes. Handle the case in which no valid order exists.
```hint What can run first
Think about which layers have no unmet inputs at the start, and what changes once one of them has run.
```
#### Clarifying Questions for this Part
- If several orders are valid, is any of them accepted, or should ties be broken deterministically, for example by insertion order?
- Should a cycle raise an exception, return an empty order, or be rejected earlier, in `addLayerOutput`?
#### What This Part Should Cover
- A correct ordering in time linear in the number of layers and edges
- Cycle detection with a useful error
- Deterministic output, and handling of layers with no connections
- Tests on the FFN example and on a graph that contains a cycle
### What a Strong Answer Covers
- Review findings ranked by impact, with C++-specific fixes
- An FFN block built with correct edge directions that the scheduler can order
- A linear-time scheduling algorithm with cycle handling
- Clean, compilable C++ throughout, explained while writing it
- The links between the parts: the invariants enforced after the review are what make construction and scheduling safe
### Follow-up Questions
- Some layers could run in parallel on the GPU. How would you group the schedule into stages of independent layers?
- How would you choose among valid orders to reduce the peak memory held by intermediate outputs?
- How would you support a layer that produces several outputs consumed by different layers?
- Should cycle detection happen in `addLayerOutput` or only when scheduling? What does each choice cost?
Overview: A C++ Network class builds a neural network as a layer graph with addLayer and addLayerOutput. Review its code quality, use the API to assemble a feed-forward block, and implement a valid layer execution order. Tests C++ ownership and invariants, graph construction, and ordering layers by their dependencies with cycle handling.