FunctionRacerProblemsRace

Coin Vending Machine

EasyClassesPython, JavaScript, TypeScript, Java, C++

Implement a class VendingMachine that sells snacks from numbered slots and takes coins. Write these methods:

slot is always a valid slot number. The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.

Example 1

VendingMachine([35,50], [2,0])
insert_coin(25) returns 25
buy(0) returns -2
insert_coin(10) returns 35
buy(0) returns 0
buy(1) returns -1
insert_coin(3) returns -1
insert_coin(25) returns 25
refund() returns 25
snacks_left(0) returns 1

25 cents is not enough for the 35 cent snack, so the first purchase gives -2. After a 10 the credit is exactly 35 and the sale leaves 0. Slot 1 is empty, a 3 cent coin is handed back, and the last 25 is refunded.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Classes problems