KatInterview

Acyclic Invitation Graph with Depth Queries and Expected Payout

Trees & GraphsAuto-graded · 18 test cases
MediumAuto-gradedTrees & Graphsgraphbfsdag

Last updated

Simulate a stream of operations on a DAG: reject cycle-forming or duplicate edges, count nodes within a hop limit, and sum expected decayed payouts using BFS shortest distances.

Invitation Graph Simulator

A hiring platform records who invited whom. Each invitation is a directed link from an inviter to an invitee. To block fraudulent invitation loops, the set of links must never contain a directed cycle.

You are given two parallel lists, operations and arguments. Process them in order, starting from an empty graph, and return a list containing one result per operation.

Operations

Each operations[i] is one of the following names, and arguments[i] is the list of its parameters.

"add_referral" with arguments [referrer, referee]

  • Try to add a directed link referrer -> referee.
  • Both users are registered in the graph as known users (if not already known), even if the link is then rejected.
  • Reject the link and return False if any of these holds:
    • referrer == referee;
    • the link already exists;
    • adding the link would create a directed cycle (that is, referee can already reach referrer through existing links).
  • Otherwise store the link and return True.

"get_reach" with arguments [user_id, max_depth]

  • Return the number of distinct users, other than user_id itself, that can be reached from user_id by following at most max_depth links. A user counts if its shortest-path distance from user_id is at most max_depth.
  • Return 0 if user_id is unknown or if max_depth <= 0.

"expected_network_value" with arguments [user_id, base_value, prob, decay]

  • Every user v reachable from user_id (excluding user_id), at shortest-path distance d >= 1, converts with probability prob. A converted user contributes base_value * decay ** (d - 1).
  • Return the expected total contribution: the sum over all reachable users v of prob * base_value * decay ** (d - 1).
  • Always use the shortest distance when several paths lead to the same user.
  • Return 0 (numerically zero) if user_id is unknown or has no reachable users.
  • Use Python's convention that 0.0 ** 0 == 1.0, so with decay = 0 only users at distance 1 contribute.

Output

Return a list of results in the same order as the operations: a boolean for add_referral, an integer for get_reach, and a number for expected_network_value.

Constraints

  • len(operations) == len(arguments).
  • User identifiers are non-empty alphanumeric strings.
  • 0.0 <= prob <= 1.0, 0.0 <= decay <= 1.0, base_value >= 0.0.
  • max_depth may be zero or negative, in which case the reach is 0.
  • Operation names are always one of the three listed above.
  • The number of users and operations is small enough that a graph traversal per operation is acceptable.

Examples

Example 1

Input: simulate_invitations(['add_referral', 'add_referral', 'add_referral', 'add_referral', 'add_referral', 'get_reach', 'get_reach', 'expected_network_value'], [['A', 'B'], ['A', 'C'], ['B', 'D'], ['C', 'D'], ['D', 'A'], ['A', 1], ['A', 2], ['A', 100, 0.5, 0.5]])

Output: [True, True, True, True, False, 2, 3, 125]

Example 2

Input: simulate_invitations(['add_referral', 'add_referral', 'get_reach', 'expected_network_value'], [['Alice', 'Bob'], ['Alice', 'Bob'], ['Unknown', 5], ['Bob', 50, 0.8, 0.9]])

Output: [True, False, 0, 0]

Hints

Hint 1

Both the cycle check and the two queries need the same thing: hop distances from a starting user. Write one reusable traversal helper.

Hint 2

Adding u -> v closes a loop exactly when v can already reach u. Run a traversal from v and see whether u shows up. Handle self-links and duplicate links before that.

Hint 3

Use BFS so the first time a user is seen gives their shortest distance. For depth-limited reach, do not expand users whose distance already equals the limit; for the payout, add prob * base_value * decay ** (d - 1) for every user at distance d >= 1.