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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

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
directed graph
cycle detection
topological sort
reverse graph
three-color DFS
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: graph[i] lists services that i calls.
  • Safe: every path from i ends 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...


Submit Your Answer
Markdown supported