Referral Credit Tracker with Indirect Referrals
Company: Databricks
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Build a referral credit tracker. Creating an account gives it an initial credit amount. If it names a referrer, that existing referrer receives the same amount. Support queries selecting up to `k` accounts whose current total credit meets a threshold. In indirect mode, each ancestor in the referral chain also receives credit.
Implement `track_referrals(operations: string[][], include_indirect: bool) -> string[][]`. This practice interface uses only string rows:
- `["add", credit, referrer]` creates the next account ID and returns a one-element row containing that ID as a decimal string. `referrer` is an existing ID or `"-1"` for no referrer.
- `["query", k, threshold]` returns a row of qualifying account IDs, encoded as decimal strings, in the order defined below.
- Every numeric input string is a valid decimal integer. There is one output row per input operation, including an empty row when a query selects no accounts.
### Constraints & Assumptions
- IDs are assigned as `0, 1, 2, ...` in creation order. Accounts are never removed or reparented.
- Credit is between 0 and 1,000,000,000. There are at most 2,000 operations. Threshold is between 0 and 2,000,000,000,000; `k` is between 0 and 2,000. Use 64-bit totals in fixed-width languages.
- Every account starts with its own initial credit. In direct mode, only its immediate referrer gets an additional copy. In indirect mode, every ancestor gets one copy of the new account's original initial credit.
- Crediting a referrer does not create another referral event. Each new account is processed once.
- For this practice version, a total meets the threshold when it is at least the threshold. Order qualifying accounts by `(total_credit, account_id)` ascending, and return the first `k`. Return fewer if insufficient accounts qualify.
- Threshold inclusivity, ordering, ID assignment, and serialization are explicit practice conventions for details not fixed by the report.
### Examples
```text
operations = [["add","10","-1"],["add","5","0"],["add","7","1"],["query","2","10"]]
include_indirect = false
result = [["0"],["1"],["2"],["1","0"]]
```
Final totals are 15, 12, and 7, so accounts 1 and 0 qualify in ascending total order.
```text
operations = [["add","10","-1"],["add","5","0"],["add","7","1"],["query","2","20"]]
include_indirect = true
result = [["0"],["1"],["2"],["0"]]
```
Indirect propagation makes the totals 22, 12, and 7. The same final query in direct mode would return an empty row.
```hint Track the source of each credit
Identify the recipients of the new account's original credit before changing totals. Do not propagate a referrer's accumulated total.
```
Overview: Build a referral credit tracker with account creation, threshold queries, deterministic ordering, and correct credit propagation through indirect referrals.
Read the full Databricks Software Engineer interview experience this question came from