Order Dependent Risk Calculations With Topological Sort
Last updated: November 29, 2026
Quick Overview
A risk engine has named calculations, each depending on the outputs of others (for example pnl depends on price and position). Return a valid evaluation order, smallest name first when several are ready, and report the calculations involved if the dependencies contain a cycle.
SIG
November 29, 20260
0
0 solved
A risk engine has named calculations, each depending on the outputs of others (for example pnl depends on price and position). Return a valid evaluation order, smallest name first when several are ready, and report the calculations involved if the dependencies contain a cycle.
SIG's trading systems recompute theoretical values, Greeks and risk in a dependency graph every time market data ticks, so dependency ordering is a natural onsite coding question. The interviewer checks Kahn's algorithm, deterministic tie-breaking, and a useful error when the configuration is cyclic.
What the Interviewer Expects
- Build the graph from both keys and values so leaf inputs are included
- Use Kahn's algorithm with in-degree counts
- Use a min-heap for deterministic order among ready nodes
- Detect a cycle when fewer nodes are emitted than exist
- Return or raise something actionable that names the stuck nodes
- Handle duplicate dependency entries
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- After a market data tick changes only one input, how do you recompute only what is affected?
- How would you run independent calculations in parallel?
- How would you report the exact cycle path rather than all stuck nodes?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: deps, a dict from calculation name to the list of names it needs. Some names appear only as dependencies (raw inputs like md). Output: a list where every calculation appears after everythin...
High-Level Design
Edges go from dependency to user. Compute in-degree = number of distinct dependencies. Start with all in-degree 0 nodes in a min-heap. Pop the smallest, append it, decrement its users, and push any th...