Maximize path score in DAG
Company: Airbnb
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in graph algorithms and optimization, focusing on reasoning about weighted directed acyclic graphs and maximizing a path score that combines node rewards and edge time costs.
Constraints
- 1 <= len(names) == len(scores) <= 200000
- 0 <= len(edges) <= 300000
- Each edge is a tuple (u, v, cost) with 0 <= u, v < n
- The graph is a directed acyclic graph (DAG)
- 0 <= cost <= 10^9
- -10^9 <= scores[i] <= 10^9
- scores[start] = 0
- At least one node whose name begins with "_" is reachable from start
- The answer fits in a signed 64-bit integer
Examples
Input: (['start', 'a', '_end', 'b'], [0, 5, 10, 4], [(0, 1, 2), (1, 2, 3), (0, 3, 1), (3, 2, 1)], 0)
Expected Output: 12
Explanation: Two main paths reach _end. Path 0->1->2 gives 0+5+10-2-3 = 10. Path 0->3->2 gives 0+4+10-1-1 = 12, which is optimal.
Input: (['_only'], [0], [], 0)
Expected Output: 0
Explanation: The start node is already a valid ending node, so the best path is the single-node path with score 0.
Hints
- Because the graph is acyclic, you can process nodes in topological order instead of using a general longest-path algorithm.
- Let best[v] be the maximum score of any path from start to v. How does best[v] update across an edge u -> v with cost c?