KatInterview

Aggregate Scores Across a Sponsorship Tree

Data Structure DesignAuto-graded · 13 test cases
MediumAuto-gradedData Structure Designtrees-graphsdesign-dsstacks-queues

Last updated

Tests tree traversal and subtree aggregation by computing cumulative scores bottom-up across a forest of sponsor-recruit relationships.

Problem Statement

A competitive gaming platform tracks players and the people who recruited them. Each player has a unique player_id, earns a certain number of points from their own activity, and was recruited by exactly one other player (sponsor_id). Founding players — those who joined without being recruited — have None as their sponsor_id.

A player's cumulative score is their own points plus the cumulative scores of every player they (directly or indirectly) recruited. In other words, it is the total points contributed by their entire recruit subtree, including themselves.

You must implement a class SponsorshipTracker that ingests all player records and answers two types of queries:

class SponsorshipTracker:
    def __init__(self, players: list[tuple[int, int, int | None]]):
        # players is a list of (player_id, points, sponsor_id)
        pass

    def get_subtree_revenue(self, player_id: int) -> int:
        # Returns the cumulative score for the given player
        pass

    def find_users_below_threshold(self, threshold: int) -> list[int]:
        # Returns a sorted list of player_ids whose cumulative score < threshold
        pass

The judge calls a single function with a flat interface (see below). Queries are issued in order; each one uses the state built from the initial player list.


Flat Interface (used by the judge)

track_referral_revenue(players, operations, arguments)
  • players: a list of [player_id, points, sponsor_id] entries, where sponsor_id is None for founding players.
  • operations: a list of operation names, each either "get_subtree_revenue" or "find_users_below_threshold".
  • arguments: a list of argument lists, one per operation.
    • "get_subtree_revenue" receives [player_id] and returns an int.
    • "find_users_below_threshold" receives [threshold] and returns a sorted ascending list[int] of player IDs.
  • Returns a list with one result per operation, in order.

Constraints

  • Number of players: up to 10^5.
  • points is a non-negative integer for every player.
  • sponsor_id is either None or the player_id of another player in the list.
  • The sponsorship structure is guaranteed to be a valid forest (one or more trees with no cycles).
  • A player whose cumulative score equals the threshold is not included in find_users_below_threshold results (strict less-than).
  • Players may appear in the input before their sponsor.
  • A find_users_below_threshold query with threshold = 0 always returns [], since points are non-negative.

Examples

Example 1

Input: track_referral_revenue([[1, 10, None], [2, 5, 1], [3, 2, 1], [4, 1, 2]], ['get_subtree_revenue', 'find_users_below_threshold'], [[2], [5]])

Output: [6, [3, 4]]

Hints

Hint 1

Build parent-child relationships in two passes over the input so that a recruit appearing before their sponsor is handled correctly.

Hint 2

A player's cumulative score can only be finalized once every player they recruited has already been finalized — process children before parents using a post-order traversal.

Hint 3

Python's recursion limit will be hit on a sponsorship chain of 100 000 players; replace recursive post-order with an explicit stack, using a boolean flag to distinguish the first visit from the return visit.