Bank Account Balances (Part 1): Latest Balance
Problem statement
This is Part 1 of the bank balance exercise. Part 2 is Monthly Bank Balance Reconciliation. The two parts can be solved independently.
You receive bank account balance records in CSV form. Each row has five fields, in this order:
bank,account,startDate,endDate,balanceFor example:
HSBC,123456,2021-12-01,2021-12-31,150
HSBC,123456,2019-05-01,2019-05-31,200
BA,566376374,2021-12-01,2021-12-31,300Given rows, bankName, and accountName, return the balance from the matching bank account's latest record. The latest record is the one with the greatest endDate. Search all records, including different months and years. Rows may arrive in any order.
Match both the bank and account exactly. Treat account names as strings, preserving leading zeros. A record's balance is the recorded balance, not an amount to add to other rows. startDate is part of the input but does not determine which record is latest.
For this practice version, rows contains data rows only, without a header. Dates use YYYY-MM-DD, balances are signed integers written in standard decimal form, and no two records for the same bank account share an endDate. Return the balance as a decimal string, or the empty string if the bank account has no record. Record periods need not be whole calendar months.
Function
latestBankBalance(rows: String[], bankName: String, accountName: String) → StringExamples
Example 1
rows = ["HSBC,123456,2021-12-01,2021-12-31,150","HSBC,123456,2019-05-01,2019-05-31,200","BA,566376374,2021-12-01,2021-12-31,300"]bankName = "HSBC"accountName = "123456"return = 150The 2021 HSBC record is later than the 2019 record, even though its balance is smaller. The BA record belongs to another bank account.
Example 2
rows = ["BANK,001,2024-12-01,2024-12-31,-20","BANK,001,2025-01-01,2025-01-31,0","BANK,1,2025-02-01,2025-02-28,999"]bankName = "BANK"accountName = "001"return = 0The latest record for account 001 has balance 0. Account 1 is a different account.
Example 3
rows = ["HSBC,123456,2021-12-01,2021-12-31,150","HSBC,123456,2019-05-01,2019-05-31,200","BA,566376374,2021-12-01,2021-12-31,300"]bankName = "HSBC"accountName = "missing"return = No row matches both the requested bank and account, so return the empty string.
Constraints
0 <= rows.length <= 100000.- Bank and account names have 1 to 20 ASCII letters, digits, hyphens, or underscores; matching is case-sensitive.
- Every data row has exactly five comma-separated fields with no surrounding spaces.
- Dates are valid ISO dates between 1900 and 2099, and
startDate <= endDate. - Each balance is a canonical decimal integer between
-1000000000and1000000000. - Within
rows, each bank/account/endDate combination is unique.