KatInterview

Filter Bank Transfers and Summarize by Source Account

Arrays & HashingAuto-graded · 15 test cases
MediumAuto-gradedArrays & Hashingsimulationhash-mapsorting

Last updated

Tests ability to validate records against a lookup table, apply multi-condition filtering, and aggregate results into ranked frequency counts.

Background

A payment processor receives a batch of transfer requests alongside a snapshot of known accounts. Before any transfer can be processed, it must pass a series of checks. Your job is to run those checks and produce a summary report.

Input

You receive two lists of dictionaries.

accounts — each entry describes one account:

KeyTypeDescription
"accountNumber"strUnique account identifier
"balance"strCurrent balance, represented as a numeric string

transactions — each entry describes one transfer attempt:

KeyTypeDescription
"fromAccountNumber"strSource account
"toAccountNumber"strDestination account
"transferAmount"strAmount to transfer, represented as a numeric string

Task

A transaction is valid if all three of the following hold:

  1. fromAccountNumber exists in accounts.
  2. toAccountNumber exists in accounts.
  3. transferAmount (as a float) is less than or equal to the balance of the source account (also parsed as a float).

Return a dictionary with exactly these three keys:

"valid_transactions" — the list of transaction dictionaries that passed all three checks, in the same order they appeared in the input.

"distinct_source_accounts" — an integer: the number of unique fromAccountNumber values that appear across the valid transactions.

"transactions_per_account" — a dictionary mapping fromAccountNumber to its count of valid transactions, limited to the top 10 source accounts by count. When two accounts have the same count, the one that comes first alphabetically appears first. The dictionary must preserve this order.

Constraints

  • 0 <= len(accounts) <= 10^4
  • 0 <= len(transactions) <= 10^5
  • All balance and transfer amount values are non-negative numeric strings parseable by float().
  • Account numbers are non-empty strings; they may contain letters and digits.
  • An account number may appear more than once in accounts; treat its first occurrence as authoritative for the balance lookup.
  • The same transaction dictionary may appear more than once in transactions; each occurrence is evaluated independently.
  • If fewer than 10 distinct source accounts exist in the valid set, return all of them.
  • If no valid transactions exist, "distinct_source_accounts" is 0 and "transactions_per_account" is {}.

Examples

Example 1

Input: banking_transaction_report([{'accountNumber': 'A1', 'balance': '100'}, {'accountNumber': 'A2', 'balance': '50'}, {'accountNumber': 'A3', 'balance': '0'}], [{'fromAccountNumber': 'A1', 'toAccountNumber': 'A2', 'transferAmount': '75'}, {'fromAccountNumber': 'A2', 'toAccountNumber': 'A3', 'transferAmount': '50'}, {'fromAccountNumber': 'A1', 'toAccountNumber': 'A3', 'transferAmount': '101'}, {'fromAccountNumber': 'A4', 'toAccountNumber': 'A1', 'transferAmount': '10'}])

Output: {'valid_transactions': [{'fromAccountNumber': 'A1', 'toAccountNumber': 'A2', 'transferAmount': '75'}, {'fromAccountNumber': 'A2', 'toAccountNumber': 'A3', 'transferAmount': '50'}], 'distinct_source_accounts': 2, 'transactions_per_account': {'A1': 1, 'A2': 1}}

Example 2

Input: banking_transaction_report([{'accountNumber': '1', 'balance': '10'}, {'accountNumber': '2', 'balance': '10'}], [{'fromAccountNumber': '1', 'toAccountNumber': '2', 'transferAmount': '10'}])

Output: {'valid_transactions': [{'fromAccountNumber': '1', 'toAccountNumber': '2', 'transferAmount': '10'}], 'distinct_source_accounts': 1, 'transactions_per_account': {'1': 1}}

Hints

Hint 1

Build a balance lookup dictionary from `accounts` first — a single pass through that list is enough, and it makes all three validation checks O(1) per transaction.

Hint 2

The three validity conditions must all be true simultaneously, so check existence for both endpoints before you attempt the float comparison to avoid a KeyError.

Hint 3

For the top-10 ranking, sort by (-count, account_number) so ties break alphabetically, then slice to 10 before building the output dictionary.