The problem wraps a pretty classic BFS in an Amazon-flavored story about microservices going down.
Model the dependency graph as an adjacency list and perform a traversal (BFS or DFS) starting from all shutdown services simultaneously to find all reachable nodes. Use a visited set to avoid cycles and ensure each affected service is returned exactly once.
Pro tip: Clarify upfront whether the shutdown list can contain duplicates or services not in the graph, and whether the output should include the shutdown services themselves—handling these edge cases shows production-level thinking.
Ask about graph size, whether the graph is guaranteed acyclic, if shutdown services should be included in the result, and how to handle duplicates or missing nodes.
Build an adjacency list (map from service to list of dependents) for efficient traversal. Use a set for shutdown services and another set for visited/affected services.
Initialize a queue (BFS) or stack (DFS) with all shutdown services. While traversing, for each neighbor not yet visited, mark it affected and add it to the traversal structure.
After traversal, return the set of affected services (excluding or including shutdown services based on clarification). Optionally sort for deterministic output.
State time complexity O(V+E) and space O(V+E). Mention that for very large graphs, distributed traversal or incremental updates could be considered.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.