KatInterview

Reconcile Ledger and Bank Snapshots for a Month

SQL & DataAuto-graded · 19 test cases
MediumAuto-gradedSQL & Datahashingjoindata-manipulation

Last updated

Join two per-month balance datasets on account id, classify each account as match, mismatch, or missing on one side, handling true zero balances correctly.

Task

A payments company keeps two independent snapshots of account balances: one from its own ledger and one from the partner bank. Each snapshot is a list of records, and a record is a dict with these keys:

  • "account_id" (str): identifier of the account.
  • "month" (str): a month string formatted as "YYYY-MM".
  • "balance_cents" (int): the closing balance in cents. It may be positive, negative, or exactly 0.

Write a function reconcile_monthly_balances(internal_records, bank_records, target_month) that compares the two snapshots for the single month target_month. Within one dataset, an account has at most one record for a given month.

The accounts that must be reported are exactly those having at least one record with month == target_month in either dataset. Records whose month differs from target_month (even for an account that also appears in the target month) are ignored.

For each such account, produce a dict with:

  • "account_id" (str)
  • "internal_balance_cents": the ledger balance, or None if the account has no ledger record for target_month
  • "bank_balance_cents": the bank balance, or None if the account has no bank record for target_month
  • "status" (str), chosen by which records are present:
    • "MATCH" — both records present and the balances are equal
    • "MISMATCH" — both records present and the balances differ
    • "MISSING_BANK" — ledger record present, no bank record
    • "MISSING_INTERNAL" — bank record present, no ledger record

Return the list of result dicts ordered by account_id using plain string (lexicographic) comparison. If no account has a record for target_month, return an empty list.

A recorded balance of 0 is a real value, not a missing record. An account with a record whose balance is 0 is treated exactly like one with any other balance.

Constraints

  • 0 <= len(internal_records) <= 100000
  • 0 <= len(bank_records) <= 100000
  • Each balance_cents value is an integer in [-10^12, 10^12]
  • All month values (records and target_month) are valid "YYYY-MM" strings
  • Each dataset contains at most one record per (account_id, month) pair
  • A balance of 0 must never be conflated with a missing record

Examples

Example 1

Input: reconcile_monthly_balances([{'account_id': 'acc_1', 'month': '2024-03', 'balance_cents': 10000}, {'account_id': 'acc_2', 'month': '2024-03', 'balance_cents': 0}, {'account_id': 'acc_3', 'month': '2024-03', 'balance_cents': 5000}, {'account_id': 'acc_1', 'month': '2024-02', 'balance_cents': 9000}], [{'account_id': 'acc_1', 'month': '2024-03', 'balance_cents': 10000}, {'account_id': 'acc_2', 'month': '2024-03', 'balance_cents': 0}, {'account_id': 'acc_4', 'month': '2024-03', 'balance_cents': 0}, {'account_id': 'acc_3', 'month': '2024-02', 'balance_cents': 5000}], '2024-03')

Output: [{'account_id': 'acc_1', 'internal_balance_cents': 10000, 'bank_balance_cents': 10000, 'status': 'MATCH'}, {'account_id': 'acc_2', 'internal_balance_cents': 0, 'bank_balance_cents': 0, 'status': 'MATCH'}, {'account_id': 'acc_3', 'internal_balance_cents': 5000, 'bank_balance_cents': None, 'status': 'MISSING_BANK'}, {'account_id': 'acc_4', 'internal_balance_cents': None, 'bank_balance_cents': 0, 'status': 'MISSING_INTERNAL'}]

Hints

Hint 1

First narrow each dataset down to the target month, then build an account_id to balance lookup for each side.

Hint 2

The set of accounts to report is the union of the account ids in the two lookups; iterate over that union in sorted order.

Hint 3

Determine the status from whether a key exists in each lookup, and use the lookup's get to fill in the balance (which yields None when absent) — never test a balance for truthiness.