Problem · Hash Table

Account Balance Manager Part 2 - Reject Overdrafts

EasyStripeONSITE INTERVIEW
See Stripe hiring insights

This continues the account balance manager. You are given a list of transactions in order, each "account_id,amount" (positive = deposit, negative = withdrawal).

Now check the balance before applying each transaction. If a withdrawal would make the account's balance go negative, reject it: do not apply it and do not change the balance. Deposits are always allowed. A transaction that leaves the balance at exactly zero is allowed.

Return the list of rejected transactions, in the order they occurred, as a String[] with each entry formatted "account_id,amount".

Examples
01 · Example 1
transactions = ["account_A,100", "account_A,-150", "account_B,50", "account_A,-80", "account_B,-100"]
return = ["account_A,-150", "account_B,-100"]
Step 1: account_A +100 -> 100 (ok). Step 2: account_A -150 -> would be -50 (REJECT). Step 3: account_B +50 -> 50 (ok). Step 4: account_A -80 -> 100-80=20 (ok). Step 5: account_B -100 -> would be -50 (REJECT). The two rejected transactions are returned in order.
02 · Example 2
transactions = ["account_A,40", "account_A,-40", "account_A,-1"]
return = ["account_A,-1"]
account_A +40 -> 40 (ok). account_A -40 -> exactly 0, which is allowed (ok). account_A -1 -> would be -1 (REJECT). Only the last transaction is rejected.
Constraints
  • Each transaction is "account_id,amount" with an integer amount.
  • Process in order; reject a transaction whose application would make the balance strictly negative.
  • Deposits are always allowed; a result of exactly 0 is allowed.
  • A rejected transaction does not change any balance.
  • Return rejected transactions in order as "account_id,amount".
More Stripe problems
drafts saved locally
public String[] rejectedTransactions(String[] transactions) {
  // write your code here
}
transactions["account_A,100", "account_A,-150", "account_B,50", "account_A,-80", "account_B,-100"]
expected["account_A", "-150", "account_B", "-100"]
sign in to submit