KatInterview

Command-Driven Ledger with Transfer Volume Leaderboard

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designhash-mapsimulationsorting

Last updated

Simulate an in-memory ledger from text commands, with validated transfers and a leaderboard ranked by outgoing volume with an alphabetical tie-break.

Command-Driven Ledger with Transfer Volume Leaderboard

You are building a small in-memory ledger. It receives a list of text commands and must process them in order, producing one output string per command.

Each account has two numbers:

  • its current balance, and
  • its sent volume, the total of all money it has successfully transferred out so far.

Both start at 0 when the account is created.

Implement run_ledger_commands(commands), which returns a list of strings. Entry i is the response to commands[i]. Tokens in a command are separated by single spaces.

Supported commands

CREATE <account_id>

  • If no account with this ID exists, create it with balance 0 and sent volume 0, and respond "true".
  • If the ID already exists, change nothing and respond "false".

DEPOSIT <account_id> <amount>

  • If the account exists and amount > 0, add amount to its balance and respond with the new balance as a decimal string.
  • If the account does not exist, or amount <= 0, change nothing and respond with the empty string "" (not "false").
  • Deposits never change sent volume.

TRANSFER <source_id> <target_id> <amount>

The transfer is valid only if all of these hold:

  • both accounts exist,
  • source_id and target_id are different,
  • amount > 0,
  • the source balance is at least amount.

If valid, subtract amount from the source balance, add it to the target balance, add amount to the source's sent volume, and respond "true". If any condition fails, change nothing and respond "false".

TOP_ACTIVITY <k>

  • Rank all existing accounts by sent volume, highest first.
  • Accounts with equal sent volume are ordered by account ID in ascending lexicographic (character code) order. For example, "B" comes before "a", and digits come before letters.
  • Respond with the IDs of the first k accounts in that order, joined by commas with no spaces, e.g. "ACC1,ACC2".
  • If there are fewer than k accounts, include all of them.
  • Accounts with zero sent volume still count and appear in the ranking.
  • If no accounts exist, respond with the empty string "".

Constraints

  • 1 <= len(commands) <= 20,000
  • Account IDs are made of letters and digits ([a-zA-Z0-9]) and have length 1 to 32.
  • 1 <= amount <= 1,000,000,000 for amounts that appear in commands.
  • 1 <= k <= 1,000
  • Balances and volumes fit in a signed 64-bit integer.
  • Every command is well formed and uses one of the four names above.

Examples

Example 1

Input: run_ledger_commands(['CREATE A', 'CREATE B', 'CREATE C', 'DEPOSIT A 100', 'DEPOSIT B 50', 'TRANSFER A B 30', 'TRANSFER B C 20', 'TRANSFER A C 10', 'TOP_ACTIVITY 2', 'TRANSFER C A 100', 'TOP_ACTIVITY 3'])

Output: ['true', 'true', 'true', '100', '50', 'true', 'true', 'true', 'A,B', 'false', 'A,B,C']

Example 2

Input: run_ledger_commands(['CREATE Z', 'CREATE A', 'TOP_ACTIVITY 2', 'DEPOSIT Z 5', 'TRANSFER Z A 5', 'TOP_ACTIVITY 2'])

Output: ['true', 'true', 'A,Z', '5', 'true', 'Z,A']

Hints

Hint 1

Keep two hash maps keyed by account ID: one for balances and one for cumulative sent volume. Create both entries together in CREATE so every account can be ranked.

Hint 2

For TRANSFER, compute all validity conditions first (both exist, different accounts, positive amount, enough balance) and only then update the three values, so a failed transfer changes nothing.

Hint 3

For TOP_ACTIVITY, sort account IDs by the pair (-sent_volume, account_id) and slice the first k. Python's default string comparison already gives the required tie-break order.