Financial Account Ledger
Learn this problemProblem statement
Process a chronological command stream for financial accounts. Implement processLedger and return every balance-query result as one comma-separated string in query order.
Commands use these forms:
INIT account_id starting_balance
FUND timestamp account_id method amount
BALANCE timestamp account_idA timestamp is either a whole-number day such as 8, or a day and hour such as 8,16. A day-only timestamp means hour 0. Day 1 is Monday; days 6 and 7 of each seven-day cycle are Saturday and Sunday.
Commands
INITcreates an account with an immediately settled starting balance. If an account is initialized more than once, only its firstINIThas an effect.FUNDadds a positive amount throughWIRE,ACH, orSTABLECOIN. Ignore funding for an unknown account. A fund contributes to balance only after it settles.BALANCEappends the account's starting balance plus every earlier accepted fund whose settlement day is at most the query day. AppendFAILUREfor an unknown account.
Effective submission and settlement days
For WIRE and ACH, first determine an effective submission day:
- A business-day request before its cutoff uses that day.
- A business-day request at or after its cutoff uses the next business day.
- A weekend request uses the following Monday, regardless of its hour.
WIRE has cutoff hour 17 and settles on its effective submission day. ACH has cutoff hour 20 and settles one business day after its effective submission day. STABLECOIN has no cutoff and settles on its request day, including weekends.
Settlement is by calendar day, not by hour. A fund that settles on day d is available to any later BALANCE command on day d, regardless of the query hour. It never affects a command that occurs earlier in the input stream.
ACH limits
Track accepted ACH principal separately for each account and bucket it by the effective submission day:
- At most
5,000,000minor units may be accepted for one effective business day. - At most
10,000,000minor units may be accepted in one seven-day week: days1–7are week 1, days8–14are week 2, and so on.
If an ACH request would exceed either limit, reject the entire request. A rejected request never settles and consumes neither limit. WIRE and STABLECOIN are unlimited.
Output
Join all BALANCE results with commas. Return the empty string when there is no BALANCE command.
Function
processLedger(commands: String[]) → StringExamples
Example 1
commands = ["INIT acct_1 10000","FUND 1 acct_1 WIRE 5000","FUND 1 acct_1 ACH 3000","BALANCE 1 acct_1","BALANCE 2 acct_1"]return = "15000,18000"The Monday wire settles on day 1. The Monday ACH settles one business day later, on day 2.
Example 2
commands = ["INIT acct_1 0","FUND 5 acct_1 WIRE 5000","BALANCE 5 acct_1","FUND 6 acct_1 STABLECOIN 1000","BALANCE 6 acct_1","FUND 6 acct_1 ACH 2000","BALANCE 8 acct_1","BALANCE 9 acct_1"]return = "5000,6000,6000,8000"The Friday wire settles that day. Stablecoin settles on Saturday. Saturday's ACH request uses Monday, day 8, as its effective submission day and settles Tuesday, day 9.
Example 3
commands = ["INIT acct_1 0","FUND 1,16 acct_1 WIRE 5000","FUND 1,17 acct_1 WIRE 3000","BALANCE 1,18 acct_1","BALANCE 2,0 acct_1"]return = "5000,8000"The 16:00 wire is before the hour-17 cutoff and settles on day 1. The 17:00 wire is at the cutoff, so it settles on day 2.
Example 4
commands = ["INIT acct_1 0","FUND 1,10 acct_1 ACH 5000000","FUND 1,11 acct_1 ACH 1000","FUND 2,11 acct_1 ACH 4000000","BALANCE 2,0 acct_1","BALANCE 3,0 acct_1","FUND 3,10 acct_1 ACH 2000000","BALANCE 4,0 acct_1"]return = "5000000,9000000,9000000"The second day-1 ACH is rejected by the daily limit. The day-2 request is accepted, bringing the week total to 9,000,000. The day-3 request is rejected because it would exceed the 10,000,000 weekly limit.
Constraints
1 <= commands.length <= 1000.- Commands are well-formed and use only the documented command types and funding methods.
- Account IDs are non-empty and contain no spaces.
- All
INITcommands precede every timestamped command. - Starting balances are nonnegative and funding amounts are positive. Every amount, balance, limit total, and intermediate value fits in a signed 64-bit integer.
- Parts using whole-day timestamps may contain multiple commands on one day; input order is their processing order. Explicit
day,hourtimestamps use hours from0through23, are strictly chronological, and never repeat. - The implementation must accept both whole-day and
day,hourtimestamps.