KatInterview

Subtree Earnings Ledger with Ranked Threshold Queries

Data Structure DesignAuto-graded · 20 test cases
MediumAuto-gradedData Structure Designhash-maptreesimulation

Last updated

Simulate a command sequence on a referral forest, maintaining subtree revenue totals and answering ranked queries filtered by a strict threshold.

Subtree Earnings Ledger

You are building an in-memory ledger for customers who may have been brought in by other customers. Each customer earns some revenue of their own, and the ledger must be able to report how much each customer's whole referral subtree has earned.

Concepts

  • Own revenue of a customer is the sum of all amounts recorded for that customer directly.
  • A customer can have a referrer, another customer who brought them in. This gives a forest: each customer has at most one referrer, and there are no cycles.
  • The subtree total of a customer is their own revenue plus the own revenue of every customer that is directly or indirectly referred by them (their children, grandchildren, and so on).

Commands

You are given two parallel lists, commands and payloads. The i-th command is executed with the i-th payload, in order. Every command produces exactly one entry in the returned list.

  1. "insert_revenue" with payload [customer_id, amount] or [customer_id, amount, referrer_id]

    • Add amount to the own revenue of customer_id.
    • If customer_id has not been seen before, it is created with own revenue 0 before the amount is added. If a referrer is given at that moment, the new customer becomes a child of that referrer.
    • If the customer already exists, any referrer in the payload is ignored; a customer's position in the forest never changes.
    • A referrer given for a new customer is guaranteed to already exist.
    • Output entry: None.
  2. "top_k_above" with payload [k, threshold]

    • Consider every existing customer whose subtree total is strictly greater than threshold.
    • Order them by subtree total from largest to smallest; customers with equal totals are ordered by customer ID ascending.
    • Return at most the first k of them, each as a pair [customer_id, subtree_total].
    • Output entry: the list of these pairs (an empty list if none qualify).

Task

Implement run_ledger(commands, payloads) that executes all commands in order and returns the list of output entries.

Constraints

  • len(commands) == len(payloads).
  • Customer IDs are all of one mutually comparable type (all integers or all strings).
  • Amounts and thresholds are numbers; k >= 0.
  • No referral cycles exist, and any referrer named for a new customer already exists.
  • A query issued before any insert returns [].
  • Aim for a design where each insert only touches the affected customer and its ancestors.

Examples

Example 1

Input: run_ledger(['insert_revenue', 'insert_revenue', 'insert_revenue', 'top_k_above'], [['A', 100], ['B', 50, 'A'], ['C', 75, 'A'], [3, 100]])

Output: [None, None, None, [['A', 225]]]

Example 2

Input: run_ledger(['insert_revenue', 'insert_revenue', 'insert_revenue', 'insert_revenue', 'top_k_above'], [[1, 20], [2, 10, 1], [3, 10, 1], [2, 5], [5, 20]])

Output: [None, None, None, None, [[1, 45]]]

Hints

Hint 1

A new amount changes the subtree total of exactly one customer plus all of that customer's ancestors. No one else is affected.

Hint 2

Store each customer's referrer and keep a running subtree total per customer; on every insert, walk up the referrer chain adding the amount.

Hint 3

For a query, filter totals with a strict greater-than, sort by (negative total, customer ID), and slice the first k. Remember that a referrer is only honored when a customer is first created.