Determine which services are affected when some services go offline.
Amazon has many services, and there are dependency relationships between them. If some of these services are taken offline, determine which other services will be affected.
Clarifications: the input is always valid (no dependency cycles), and the input format is up to the candidate. I defined it as a hash map where the key is the service name and the value is the list of services that depend on it. The problem is essentially topological sorting.
Approach: compute the in-degree of each service; add all services with in-degree 0 to a queue; start a set of affected services with the services taken offline; process the queue: pop a service, and if it is in the affected set, add its dependents to the affected set; decrease the in-degree of the dependents and enqueue any that reach 0; when the queue is empty, return the affected set.
Format · Amazon SDE intern second round.
Community answers
Related courses


