Member-only story
Coding Interview Prep: Design an ATM Machine (JavaScript)
Coding Prompt
This problem presents a simulation of an ATM machine that operates with five denominations of banknotes: 20, 50, 100, 200, and 500 dollars. The ATM starts out empty and supports two primary operations: depositing and withdrawing money. When depositing, the user increases the count of each denomination in a specific order. Withdrawing involves the user requesting a certain amount of money, and the ATM provides this amount using the largest denomination banknotes first. The ATM tries to give the user as little banknotes as possible, ensuring the total amount of banknotes provided sums up to the amount requested. If the ATM cannot provide the exact amount with the banknotes available (without splitting a banknote), the withdraw request is rejected and should return [-1]
without any banknotes being dispensed.
Coding Solution (JavaScript)
class ATM {
constructor(denominations){
this.denominations = denominations;
this.banknotesCount = new Array(denominations.length).fill(0);
}
deposit(banknotesCount) {
for (let i = 0; i < this.denominations.length; i++) {
this.banknotesCount[i] += banknotesCount[i];
}
}
withdraw(amount) {
const dispensed = new Array(this.denominations.length).fill(0);
for (let i = this.denominations.length - 1; i >= 0; i--) {
const denomination = this.denominations[i];
const count =…