AccountBalance
SRM 288 · 2006-02-08 · by erinn
Problem Statement
You are working for the financial institution TopBank, and you have been tasked with writing a module that will take an initial account balance, along with a list of that day's transactions, and return the ending balance for the day.
Each transaction will be either a credit, which adds funds to the account, or a debit, which removes funds from the account. If a debit exceeds the available funds at the time, then the account balance will go negative. You will be given an
Constraints
- startingBalance will be between 0 and 1000000, inclusive.
- transactions will have between 0 and 50 elements, inclusive.
- Each element of transactions will be formatted as "type amount" (quotes added for clarity).
- Each type will be 'C' or 'D'.
- Each amount will represent an integer between 1 and 1000000, inclusive, with no leading zeros.
100
{"C 1000", "D 500", "D 350"}
Returns: 250
This person had 100 dollars, got their paycheck, then went shopping at two different stores. 100 + 1000 - 500 - 350 = 250.
100
{}
Returns: 100
With no transactions, the balance doesn't change by the end of the day.
100
{"D 50", "D 20", "D 40"}
Returns: -10
Uh oh! This person's account is overdrawn.
53874
{"D 1234", "C 987", "D 2345", "C 654", "D 6789", "D 34567"}
Returns: 10580
Several transactions of both types.
1000000
{"C 1000000", "C 1000000", "C 1000000", "C 1000000", "C 1000000",
"C 1000000", "C 1000000", "C 1000000", "C 1000000", "C 1000000",
"C 1000000", "C 1000000", "C 1000000", "C 1000000", "C 1000000",
"C 1000000", "C 1000000", "C 1000000", "C 1000000", "C 1000000",
"C 1000000", "C 1000000", "C 1000000", "C 1000000", "C 1000000",
"C 1000000", "C 1000000", "C 1000000", "C 1000000", "C 1000000",
"C 1000000", "C 1000000", "C 1000000", "C 1000000", "C 1000000",
"C 1000000", "C 1000000", "C 1000000", "C 1000000", "C 1000000",
"C 1000000", "C 1000000", "C 1000000", "C 1000000", "C 1000000",
"C 1000000", "C 1000000", "C 1000000", "C 1000000", "C 1000000"}
Returns: 51000000
Submissions are judged against all 35 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class AccountBalance with a public method int processTransactions(int startingBalance, vector<string> transactions) · 35 test cases · 2 s / 256 MB per case