Find the longest follow chain from any user in a directed social graph with cycles

Quick 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.

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

You are given a social graph of follower relationships, represented as a directed graph. `graph` maps each user ID to the list of user IDs that user follows. Use recursion to find the maximum depth of a follow chain: the longest chain of follows starting from any user in the graph. A chain moves from a user to someone that user follows, then to someone that person follows, and so on. If the graph contains cycles, do not revisit users already in the current follow chain. The depth is the number of edges (follow relationships) in the chain. ### Function Signature ```python def max_follow_depth(graph: dict[int, list[int]]) -> int: ``` ### Rules - `graph[u]` lists the users that `u` follows. Each edge points from the follower to the user being followed. - A user who appears only inside follow lists follows nobody. - A chain never contains the same user twice. A chain of a single user has depth 0, so the answer is 0 when nobody follows anyone. - The chain may start at any user. Return the largest depth over all starting users. ### Constraints - `1 <= number of users <= 1000` - `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 ```text Input: graph = {0: [1, 2], 1: [3], 2: [4], 3: [], 4: [5], 5: []} Output: 3 ``` From user 0 the chains are `0 -> 1 -> 3` with 2 edges and `0 -> 2 -> 4 -> 5` with 3 edges. No other starting user does better, so the answer is 3. ```text Input: graph = {0: [1], 1: [2], 2: [0]} Output: 2 ``` Users 0, 1 and 2 form a cycle. From any of them the chain visits the other two and then stops, because the next follow leads back to a user already in the chain. That gives 2 edges. ```text Input: graph = {0: [1], 1: [2], 2: [1], 3: [0]} Output: 3 ``` The longest chain is `3 -> 0 -> 1 -> 2`. It starts at user 3 and stops at user 2, whose only follow is user 1, who is already in the chain.

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.

Roblox logo
Roblox
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a social graph of follower relationships, represented as a directed graph. graph maps each user ID to the list of user IDs that user follows. Use recursion to find the maximum depth of a follow chain: the longest chain of follows starting from any user in the graph.

A chain moves from a user to someone that user follows, then to someone that person follows, and so on. If the graph contains cycles, do not revisit users already in the current follow chain. The depth is the number of edges (follow relationships) in the chain.

Function Signature

def max_follow_depth(graph: dict[int, list[int]]) -> int:

Rules

  • graph[u] lists the users that u follows. Each edge points from the follower to the user being followed.
  • A user who appears only inside follow lists follows nobody.
  • A chain never contains the same user twice. A chain of a single user has depth 0, so the answer is 0 when nobody follows anyone.
  • The chain may start at any user. Return the largest depth over all starting users.

Constraints

  • 1 <= number of users <= 1000
  • 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: graph = {0: [1, 2], 1: [3], 2: [4], 3: [], 4: [5], 5: []}
Output: 3

From user 0 the chains are 0 -> 1 -> 3 with 2 edges and 0 -> 2 -> 4 -> 5 with 3 edges. No other starting user does better, so the answer is 3.

Input: graph = {0: [1], 1: [2], 2: [0]}
Output: 2

Users 0, 1 and 2 form a cycle. From any of them the chain visits the other two and then stops, because the next follow leads back to a user already in the chain. That gives 2 edges.

Input: graph = {0: [1], 1: [2], 2: [1], 3: [0]}
Output: 3

The longest chain is 3 -> 0 -> 1 -> 2. It starts at user 3 and stops at user 2, whose only follow is user 1, who is already in the chain.