KatInterview

Credit Ledger Simulator with Merges and Delayed Rewards

Data Structure DesignAuto-graded · 13 test cases
HardAuto-gradedData Structure Designdesignunion-findheap

Last updated

Simulate a timestamped ledger with deposits, transfers, spends with delayed cashback, account merges and spend rankings, using union-find and a min-heap.

Credit Ledger Simulator

You are building the engine behind a shared credit ledger for team workspaces. Each workspace is identified by a string ID and holds a non-negative integer balance of credits. You are given a batch of commands that must be executed in order, and you must return the result of each one.

Input

  • commands: a list of command names.
  • params: a list of parameter lists. params[i] holds the arguments for commands[i]. The last element of every parameter list is the timestamp of that command. Timestamps never decrease from one command to the next.

The supported commands and their parameter lists are:

CommandParametersResult
"deposit"[workspace_id, amount, timestamp]bool
"spend"[workspace_id, amount, cashback_percent, delay, timestamp]bool
"transfer"[from_id, to_id, amount, timestamp]bool
"merge"[source_id, target_id, timestamp]bool
"get_balance"[workspace_id, timestamp]int
"get_top_spenders"[k, timestamp]list[str]

Output

Return a list with one result per command, in the same order.

Rules

Matured cashback. A cashback scheduled for time T becomes part of the balance as soon as the clock reaches T. Before executing any command with timestamp t, every pending cashback with maturity time <= t must already be credited. This includes commands that run at exactly the maturity time, so a spend or transfer at time t can use cashback maturing at t.

deposit. Adds amount to the workspace. If the ID does not exist yet, the workspace is created with that amount. If the ID is an alias left over from a merge, the credits go to the workspace it now resolves to, and the old ID is not recreated. Always returns True.

spend. Succeeds only if the workspace exists and its balance (including matured cashback) is at least amount. On success:

  • amount is subtracted from the balance and added to the workspace's cumulative spend.
  • A cashback of amount * cashback_percent // 100 (integer floor division) is scheduled to mature at timestamp + delay.
  • The command returns True.

Otherwise nothing changes and it returns False. Spending the exact full balance is allowed.

transfer. Moves amount from from_id to to_id. It returns True only if both IDs exist, they resolve to different workspaces, and the source balance is at least amount. Otherwise nothing changes and it returns False. A transfer never creates a workspace, and it does not count as spending.

merge. Folds source_id into target_id.

  • It returns False if either ID does not exist, or if both IDs resolve to the same workspace.
  • Otherwise the source's current balance, its cumulative spend, and all of its still-pending cashbacks become the target's. From then on, every use of the source ID (or any ID that already pointed to it) refers to the target. The target's ID remains the canonical ID.
  • It returns True.

If a given ID was itself merged earlier, a merge involving it acts on the workspace it currently resolves to.

get_balance. Returns the workspace's balance at timestamp, including matured cashback, or -1 if the ID does not exist. Aliases resolve to their current workspace.

get_top_spenders. Returns up to k canonical workspace IDs, ordered by cumulative spend (all spends up to and including timestamp) from largest to smallest. Ties are broken by canonical ID in ascending lexicographic order. Only workspaces whose cumulative spend is greater than 0 are eligible, so the list may be shorter than k, or empty.

Constraints

  • Up to 50,000 commands.
  • 1 <= timestamp <= 10^9, and timestamps are non-decreasing across the batch.
  • 1 <= amount <= 10^6
  • 0 <= cashback_percent <= 100
  • 1 <= delay <= 10^6
  • 1 <= k <= 100
  • Workspace IDs are alphanumeric strings of length 1 to 32 (hyphens may also appear in test data).
  • Every command name is one of the six listed above.

Examples

Example 1

Input: run_credit_ledger(['deposit', 'spend', 'get_balance', 'get_balance', 'deposit', 'merge', 'get_balance', 'get_top_spenders'], [['team-a', 100, 1], ['team-a', 40, 10, 5, 2], ['team-a', 4], ['team-a', 7], ['team-b', 50, 8], ['team-a', 'team-b', 9], ['team-a', 10], [2, 10]])

Output: [True, True, 60, 64, True, True, 114, ['team-b']]

Hints

Hint 1

Handle merges with a union-find structure keyed by workspace ID. Always make the merge target the root, since its ID is the canonical one, and resolve every ID through find before using it.

Hint 2

Keep all scheduled cashbacks in a single min-heap ordered by maturity time. Before running each command, pop everything with maturity <= the command's timestamp and credit it.

Hint 3

Credit a matured cashback to the workspace its ID resolves to at maturity time, not at scheduling time. Then pending cashbacks follow merges automatically, with no extra work during the merge.

Hint 4

Store balance and cumulative spend only for root workspaces. On a merge, add the source's values into the target and delete the source's entries. For top spenders, filter spend > 0 and sort by (-spend, id).