Find the longest follow chain from any user in a directed social graph with cycles
Company: Roblox
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
Overview: Given a directed follow graph that may contain cycles, return the length in edges of the longest follow chain that never repeats a user, starting from any user. It tests recursive graph search, cycle handling and an honest complexity analysis.
Constraints
- `1 <= number of users <= 1000`, where the number of users is `n = len(graph)`
- `0 <= len(graph[u]) <= 100` for every user `u`
- User IDs are integers from `0` to `n - 1`, where `n` is the number of users.
- The graph may contain cycles.
Examples
Input: ([[]],)
Expected Output: 0
Explanation: Minimum valid input: one user who follows nobody, so the only chain has depth 0.
Input: ([[0]],)
Expected Output: 0
Explanation: A single user who follows only themselves. Following that edge would repeat the user, so the depth stays 0.
Hints
- The best chain does not have to start at user 0, so consider every user as a possible start.
- Only the users in the current chain are off limits. A user skipped on one chain can still appear on a different chain.
- Depth counts follow edges, so a chain of k users has depth k - 1.