Find maximum follow depth using recursion
Problem Description
You are given a directed follows relationship representing a social graph. Each record (follower_id, followee_id) indicates that follower_id follows followee_id. This relationship should be treated as a directed graph.
Task
Implement a function that, given:
- A list of follow edges
follows = [(follower_id, followee_id), ...] - A starting user
start_id
returns the maximum number of follow "layers" reachable from start_id by repeatedly following the next user. Formally, this means computing the length of the longest directed path starting at start_id.
- Layer 1: Users directly followed by
start_id - Layer 2: Users followed by those users (from Layer 1)
- ...
The function should return the maximum layer count reachable.
Requirements and Edge Cases
- The implementation must use recursion. Memoization is permitted and encouraged.
- The solution must correctly handle cycles (e.g., A→B→C→A) without resulting in infinite recursion.
- If
start_idfollows nobody, the function should return0.
Example
Given the follow edges:
1→22→33→4
For start_id = 1, max_depth(1) should return 3 (layers: {2}, {3}, {4}).
Given the follow edges:
1→22→1
For start_id = 1, max_depth(1) should return 1 (due to the cycle, the path 1->2 has length 1, and 1->2->1 would be a cycle, so we don't count 1 again as a new layer).
AI interview prep
Core dilemma
Efficiently finding the longest path in a directed graph while strictly adhering to recursion, preventing infinite loops in cycles, and optimizing for potentially large social graphs.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Find maximum follow depth using recursion
Problem Description
You are given a directed follows relationship representing a social graph. Each record (follower_id, followee_id) indicates that follower_id follows followee_id. This relationship should be treated as a directed graph.
Task
Implement a function that, given:
- A list of follow edges
follows = [(follower_id, followee_id), ...] - A starting user
start_id
returns the maximum number of follow "layers" reachable from start_id by repeatedly following the next user. Formally, this means computing the length of the longest directed path starting at start_id.
- Layer 1: Users directly followed by
start_id - Layer 2: Users followed by those users (from Layer 1)
- ...
The function should return the maximum layer count reachable.
Requirements and Edge Cases
- The implementation must use recursion. Memoization is permitted and encouraged.
- The solution must correctly handle cycles (e.g., A→B→C→A) without resulting in infinite recursion.
- If
start_idfollows nobody, the function should return0.
Example
Given the follow edges:
1→22→33→4
For start_id = 1, max_depth(1) should return 3 (layers: {2}, {3}, {4}).
Given the follow edges:
1→22→1
For start_id = 1, max_depth(1) should return 1 (due to the cycle, the path 1->2 has length 1, and 1->2->1 would be a cycle, so we don't count 1 again as a new layer).