Simple Bank System

MediumDesignsimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A bank has n accounts numbered 1 to n; the starting balance of account i is balance[i - 1]. Implement Bank(balance) with three operations, each returning true if it succeeded and false if it was rejected (a rejected operation changes nothing): - transfer(account1, account2, money) moves money from account1 to account2. - deposit(account, money) adds money to account. - withdraw(account, money) removes money from account. An operation is rejected if it names an account outside 1..n, or if it would take more money out of an account than that account currently holds. Transferring to the same account is allowed and leaves the balance unchanged when it succeeds.

Examples

Input: ["Bank","deposit","transfer","withdraw","transfer","withdraw","transfer"] [[[50,5,0]],[3,25],[1,2,45],[2,51],[3,4,1],[1,5],[2,2,50]]
Output: [null,true,true,false,false,true,true]
Balances become [50,5,25], then [5,50,25]. Account 2 cannot give 51. Account 4 does not exist. Withdrawing 5 empties account 1. Account 2 can send its 50 to itself.
Input: ["Bank","withdraw","transfer","deposit","deposit","withdraw","withdraw"] [[[0]],[1,1],[1,1,1],[0,5],[1,7],[1,7],[1,1]]
Output: [null,false,false,false,true,true,false]
The only account starts empty, so withdrawing or transferring fails. Account 0 does not exist. After depositing 7 it can withdraw exactly 7, then nothing more.

Constraints

  • 1 <= n == balance.length <= 10^5
  • 0 <= balance[i] <= 10^12
  • 1 <= money <= 10^12
  • 1 <= account, account1, account2 <= 10^5
  • At most 10^4 calls in total

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems