Find Services That Can Never Get Stuck in a Retry Loop
Last updated: November 29, 2026
Quick Overview
Services form a directed call graph. A service is safe if every possible chain of calls starting from it eventually reaches a service that calls nothing. Return all safe services in ascending order.
0
0
0 solved
Services form a directed call graph. A service is safe if every possible chain of calls starting from it eventually reaches a service that calls nothing. Return all safe services in ascending order.
An onsite question framed around microservices at Google scale, where a cyclic call chain can cause unbounded retries. The interviewer looks for either reverse-graph Kahn's algorithm or three-color DFS, and an explanation of why nodes that can reach a cycle are unsafe.
What the Interviewer Expects
- Define safe as: cannot reach any cycle
- Use three-color DFS or reverse Kahn's algorithm
- Mark terminal nodes as safe base cases
- Propagate safety backward using out-degree counts
- Return sorted output
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return the services that are part of a cycle versus merely reaching one.
- Handle edges being added in real time.
- What if only some call edges are retried?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
graph[i]lists services thaticalls. - Safe: every path from
iends at a terminal node (no outgoing edges). - Output: sorted list of safe nodes....
High-Level Design
A node is unsafe exactly when it can reach a cycle. Reverse Kahn's algorithm captures that cleanly. Build the reverse graph and keep each node's out-degree in the original graph. Terminal nodes (out-d...