Find Affected Services After Shutdown
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of graph traversal, reachability, and dependency analysis by measuring the ability to identify transitive impacts across a directed acyclic service dependency graph.
Constraints
- 0 <= number of services <= 100000
- 0 <= total dependency relationships <= 200000
- Service names are strings
- The dependency graph has no cycles
Examples
Input: ({'A': ['B', 'C'], 'B': ['D'], 'C': ['E'], 'D': [], 'E': []}, {'A'})
Expected Output: {'A', 'B', 'C', 'D', 'E'}
Explanation: Stopping A directly affects B and C, which then affect D and E.
Input: ({'Auth': ['Checkout', 'Profile'], 'Checkout': ['Payments'], 'Profile': ['Settings'], 'Payments': ['Ledger'], 'Settings': ['Ledger'], 'Ledger': []}, {'Auth', 'Settings'})
Expected Output: {'Auth', 'Checkout', 'Profile', 'Payments', 'Settings', 'Ledger'}
Explanation: Auth affects Checkout and Profile, which lead to Payments, Settings, and Ledger. Settings is also initially stopped.
Input: ({'A': ['B'], 'B': []}, set())
Expected Output: set()
Explanation: If no services are stopped, then no services are affected.
Input: ({'A': ['B', 'B'], 'B': ['C']}, {'A', 'X'})
Expected Output: {'A', 'B', 'C', 'X'}
Explanation: A affects B and then C. X is included even though it does not appear in the map. The duplicate edge to B should not change the result.
Hints
- Model the services as a directed graph and start a traversal from every service in `stopped`.
- Use a visited set so that shared downstream services are only processed once.