KatInterview

Referral Credit Ledger with Multilevel Propagation

Trees & GraphsAuto-graded · 18 test cases
MediumAuto-gradedTrees & Graphstrees-graphsarraysdesign-ds

Last updated

Simulate account additions, referral bonus propagation (direct or indirect), and threshold queries ordered deterministically.

Design a system that tracks account balances earned through a referral program.

Accounts are assigned sequential zero-indexed integer IDs (0, 1, 2, ...) as they are created. Upon creation, an account receives an initial credit amount. Additionally, if the new account specifies a valid referrer, credit bonus points are awarded up the referral chain.

You are given a sequence of instructions operations and a boolean flag include_indirect.

Each instruction in operations is represented as a list of strings:

  • ["add", credit, referrer]
    • Registers a new account with an initial credit (given as a numeric string).
    • referrer is the integer ID string of an existing account, or "-1" if the new account has no referrer.
    • The newly created account ID is returned as a single-element list of strings [id_str].
    • Credit Propagation Rule: The initial credit of the new account is awarded to its referrer chain.
      • If include_indirect is False, only the immediate referrer receives the bonus.
      • If include_indirect is True, the immediate referrer and all of its ancestors up to the root receive the bonus.
      • Note: Updating a referrer's balance does not trigger sub-referral events; each add operation propagates only the original initial credit of the new account.
  • ["query", k, threshold]
    • Queries existing accounts whose total accumulated balance is at least threshold (parsed as an integer).
    • Filtered accounts are ordered primarily by total_credit ascending, and secondarily by account_id ascending.
    • Returns a list containing the string representation of the IDs of up to the first k qualifying accounts. If fewer than k accounts qualify, return all qualifying account IDs. If no accounts qualify, return an empty list [].

The function must return a list of string lists, where each entry corresponds to the result of the instruction at the same index.

Constraints

  • $0 \le \text{len(operations)} \le 2,000$
  • $0 \le \text{credit} \le 1,000,000,000$
  • $0 \le k \le 2,000$
  • $0 \le \text{threshold} \le 2,000,000,000,000$
  • referrer is guaranteed to be a valid existing account ID or "-1".
  • Accounts are never deleted or re-parented.

Examples

Example 1

Input: track_referrals([['add', '10', '-1'], ['add', '5', '0'], ['add', '7', '1'], ['query', '2', '10']], False)

Output: [['0'], ['1'], ['2'], ['1', '0']]

Example 2

Input: track_referrals([['add', '10', '-1'], ['add', '5', '0'], ['add', '7', '1'], ['query', '2', '20']], True)

Output: [['0'], ['1'], ['2'], ['0']]

Hints

Hint 1

Sequential IDs starting at 0 mean account data can be indexed directly by integer array indices without a hash map.

Hint 2

When propagating bonus credits, pass only the original initial credit of the newly added account—do not propagate the accumulated totals of parents.

Hint 3

Since constraints are modest (up to 2,000 operations), filtering and sorting the accounts on every query operation easily runs within time limits.