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.

You are given a social graph of follower relationships, represented as a directed graph. `graph[u]` is the list of user IDs that user `u` follows, so each edge points from the follower to the user being followed. 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. ### Input format The original prompt passes a dictionary that maps each user ID to that user's follow list. User IDs are exactly `0` to `n - 1`, so this console passes `graph` as a list of `n` lists, and `graph[u]` is the follow list of user `u`. A user who appears only inside other users' follow lists follows nobody, so that user's row is the empty list `[]`. ### Rules - 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. - A follow list may repeat a user or list `u` itself. Neither one can make a chain longer, because a chain never contains the same user twice. The original prompt asks for a recursive solution. Only the returned integer is graded. With up to 1000 users a chain can have 999 edges, so a recursive Python solution may need a higher recursion limit. The answer is at most `n - 1`, which fits in a 32-bit integer. ### Examples ```text Input: graph = [[1, 2], [3], [4], [], [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 = [[1], [2], [1], [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.

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

  1. The best chain does not have to start at user 0, so consider every user as a possible start.
  2. Only the users in the current chain are off limits. A user skipped on one chain can still appear on a different chain.
  3. Depth counts follow edges, so a chain of k users has depth k - 1.

Longest Follow Chain in a Social Graph

mediumRobloxSoftware Engineer

You are given a social graph of follower relationships, represented as a directed graph. graph[u] is the list of user IDs that user u follows, so each edge points from the follower to the user being followed. 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.

Input format

The original prompt passes a dictionary that maps each user ID to that user's follow list. User IDs are exactly 0 to n - 1, so this console passes graph as a list of n lists, and graph[u] is the follow list of user u. A user who appears only inside other users' follow lists follows nobody, so that user's row is the empty list [].

Rules

  • 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.
  • A follow list may repeat a user or list u itself. Neither one can make a chain longer, because a chain never contains the same user twice.

The original prompt asks for a recursive solution. Only the returned integer is graded. With up to 1000 users a chain can have 999 edges, so a recursive Python solution may need a higher recursion limit. The answer is at most n - 1, which fits in a 32-bit integer.

Examples

Input: graph = [[1, 2], [3], [4], [], [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 = [[1], [2], [1], [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.

Examples

Example 1
Input
([[]],)
Output
0
Notes
Minimum valid input: one user who follows nobody, so the only chain has depth 0.
Example 2
Input
([[0]],)
Output
0
Notes
A single user who follows only themselves. Following that edge would repeat the user, so the depth stays 0.

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def max_follow_depth(graph: list[list[int]]) -> int:
Sample call
max_follow_depth([[]])
Language
Case 1
Input
([[]],)
Expected
0
Case 2
Input
([[0]],)
Expected
0

Browse More Questions

Show the approach

Approach

A search that tries every chain from every start is correct, but it can take exponential time even on graphs without cycles, because the number of distinct chains can grow like the Fibonacci numbers. The reference first drops self-follows and repeated entries, since neither can extend a chain. It then finds the strongly connected components with an iterative Tarjan pass.

Once a chain leaves a component it can never come back: a user reachable from the later part of the chain that could also reach back into the component would belong to that component. So the best chain from user v is a simple path inside v's component that ends at some user u, followed either by nothing or by one edge u -> w into a later component and then the best chain from w. That last part does not depend on which users came earlier, so it is stored once as best[w].

Tarjan completes components in reverse topological order, so when a component completes, best[w] is already known for every edge that leaves it. For each member u, exit[u] is the larger of 0 and the maximum of 1 + best[w] over u's leaving edges. For each start s in the component, an explicit-stack enumeration of simple paths that stay inside the component tracks only the users on the current path and records depth + exit[end]. That maximum is best[s]. A single-user component needs no enumeration, so chains and DAGs run in linear time, and only cyclic components pay for path enumeration. The answer is the largest best[s], and the search stops early once it reaches n - 1, the most edges any chain can have.

Edge cases: a single user, users with empty rows, a graph with no follows at all (answer 0), self-follows, repeated entries, a best chain that starts at a high-ID user nobody follows, and chains of up to 999 edges, which the explicit stacks handle without any recursion limit.

Time complexity:
O(n + E + P), where E is the total length of all follow lists and P is the number of search steps spent enumerating simple paths inside cyclic strongly connected components (exponential in the worst case, since longest simple path is NP-hard in general; linear for chains and DAGs)
Space complexity:
O(n + E)