Compute dependency load factors in a DAG
Company: Robinhood
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of graph theory and algorithms, specifically reachability and dependency analysis in a directed acyclic graph, along with attention to input validation and set-based reasoning.
Constraints
- 0 <= len(programs) <= 500
- 0 <= len(prerequisites) <= 5000
- programs contains unique strings
- After ignoring invalid pairs, the remaining dependency graph is a DAG
- Duplicate prerequisite pairs may appear and should not change the result
Examples
Input: (['A', 'B', 'C', 'D'], [['A', 'B'], ['B', 'C'], ['A', 'C'], ['D', 'C'], ['E', 'A']])
Expected Output: {'A': 0, 'B': 1, 'C': 3, 'D': 0}
Explanation: ['E', 'A'] is ignored because E is not in programs. C is depended on by A, B, and D; B is depended on by A; A and D are not depended on by any listed program.
Input: ([], [['A', 'B']])
Expected Output: {}
Explanation: There are no valid programs, so the result is an empty dictionary.
Input: (['X'], [['X', 'Y'], ['Y', 'X'], ['Z', 'W']])
Expected Output: {'X': 0}
Explanation: All prerequisite pairs are ignored because at least one endpoint is not in programs. X has no dependents.
Input: (['P', 'Q', 'R', 'S', 'T'], [['P', 'Q'], ['P', 'R'], ['Q', 'S'], ['R', 'S'], ['T', 'S'], ['P', 'Q']])
Expected Output: {'P': 0, 'Q': 1, 'R': 1, 'S': 4, 'T': 0}
Explanation: The duplicate pair ['P', 'Q'] does not change the graph. S is depended on by P, Q, R, and T. Q and R are each depended on only by P.
Input: (['alpha', 'beta', 'gamma'], [])
Expected Output: {'alpha': 0, 'beta': 0, 'gamma': 0}
Explanation: With no dependencies, every program has Load Factor 0.
Hints
- Ignore invalid pairs first. For every valid edge a -> b, any program that can reach a can also reach b.
- Since the graph is a DAG, process nodes in topological order and propagate ancestor information forward.