KatInterview

Compute Direct and Total Network Reach for Each User

Trees & GraphsModel answer
MediumModel answerTrees & GraphsgraphsBFSreachability

Last updated

Given a list of sponsor–member pairs, return the direct and full downstream unique reach count for every user, handling duplicates, converging paths, and cycles.

Background

A peer-to-peer network tracks how members join through existing users. Each join event is recorded as a (sponsor, member) pair meaning sponsor brought member into the network. Your task is to analyze the influence reach of every user in this dataset.

For each user, compute two numbers:

  • Direct count: how many unique people this user personally sponsored.
  • Network count: how many unique people are reachable from this user through one or more sponsorship steps, not counting the user themselves.

The data is messy. You must handle all of the following:

  1. The same pair can appear more than once (duplicate records).
  2. A single person can be sponsored by multiple users (converging paths).
  3. Sponsorship chains can loop back on themselves (cycles).
  4. A user must never be counted in their own reach, even if a cycle connects back to them.
  5. Every user who appears in any position — sponsor or member — must have an entry in the output.

Function Signature

from typing import List, Tuple, Dict

def compute_reach(pairs: List[Tuple[str, str]]) -> Dict[str, Dict[str, int]]:
    pass

Input & Output

  • Input: pairs — a list of (sponsor, member) tuples where each value is a non-empty alphanumeric string representing a user ID.
  • Output: a dictionary mapping each user ID to {"direct": int, "network": int}.

Constraints

  • 0 <= len(pairs) <= 5,000
  • Number of unique users N <= 1,000
  • User IDs are non-empty alphanumeric strings.

Examples

Example 1 — Converging paths

pairs = [
    ("A", "B"),
    ("A", "C"),
    ("B", "D"),
    ("C", "D"),
]

compute_reach(pairs)
# {
#   "A": {"direct": 2, "network": 3},  # reaches B, C, D
#   "B": {"direct": 1, "network": 1},  # reaches D
#   "C": {"direct": 1, "network": 1},  # reaches D
#   "D": {"direct": 0, "network": 0},
# }

Note that D is reachable from A via two paths but is counted only once.

Example 2 — Cycles and duplicates

pairs = [
    ("A", "B"),
    ("B", "C"),
    ("C", "A"),
    ("A", "B"),  # duplicate
    ("C", "D"),
]

compute_reach(pairs)
# {
#   "A": {"direct": 1, "network": 3},  # reaches B, C, D (not itself)
#   "B": {"direct": 1, "network": 3},  # reaches C, D, A (not itself)
#   "C": {"direct": 2, "network": 3},  # reaches A, B, D (not itself)
#   "D": {"direct": 0, "network": 0},
# }

Edge Cases to Consider

  • Empty input → return {}.
  • A self-sponsorship like ("A", "A") → A gets direct: 0, network: 0 (unless other edges exist).
  • Users who only appear as members (never as a sponsor) still get an entry with zero counts.
  • Disconnected subgraphs.

Hints

Hint 1

Model the pairs as a directed graph. Direct count is simply the out-degree of each node once you've stripped duplicates and self-loops. What data structure removes duplicates automatically?

Hint 2

For network count, think about what 'all reachable nodes' means on a graph that may have cycles. A per-source BFS or DFS with a visited set gives you exactly the reachable set — how do you make sure the source node itself is never counted, even when a cycle circles back to it?

Hint 3

Summing children's network counts is tempting but wrong: if two paths converge on the same node it gets counted twice. The visited-set approach sidesteps this entirely. Once you have the visited set after BFS, the network count is just its size minus one.