Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-256

Bank Account Invariant Check

Problem

Check the never-negative balance invariant on a bank account.

start_balance is the opening balance. operations contains ["deposit", amount], ["withdraw", amount], or ["balance"]. A withdrawal succeeds only when it keeps the balance non-negative.

Return the list of recorded results (None for deposit, True or False for withdraw, the balance for balance).

Hint

A rejected withdrawal must leave the balance untouched.

Test cases

case 1
in: [100,[["withdraw",30],["withdraw",80],["balance"],["deposit",5]]]
expected: [true,false,70,null]
case 2
in: [0,[["withdraw",1],["balance"]]]
expected: [false,0]
case 3
in: [50,[["balance"]]]
expected: [50]
case 4
in: [10,[["deposit",0],["balance"]]]
expected: [null,10]
Starter codePython
def bank_invariant(start_balance, operations):
    # Your code here
    pass

4 test cases · run your code against them in the editor