This is a design round. You write the classes, and the interviewer checks that they behave.
Here you build a cash machine, ATM. It knows a set of bank accounts. It also has a box of notes worth 100, 50, 20 and 10.
Keep each account in its own class, Account. Each account belongs to one card. It has a PIN and a balance, and it counts the wrong PINs typed for it.
Implement the ATM class:
NewATM()starts with no accounts, an empty box and no card inside.addAccount(card, pin, balance)adds an account for the cardcard.load(note, count)putscountnotes worthnotein the box.insert(card)puts a card in the machine. It returnstrueif the card is taken.enterPin(pin)checks a PIN for the card inside. It returnstrueif the PIN is right.balance()returns the balance of the account that is signed in.withdraw(amount)pays out cash. It returns the notes given, or "denied".eject()gives the card back. It returnstrueif a card was inside.
Go has no classes. Account and ATM are structs, made by NewAccount and NewATM. Their methods have pointer receivers, because they change them.
The machine is in one of three states. It is idle with no card. Or it has a card, but nobody has signed in. Or someone has signed in with that card.
insert works only when no card is inside. The card must belong to an account, and that account must not be locked. Then the card is inside, and nobody is signed in yet. Otherwise insert returns false and changes nothing.
enterPin works only when a card is inside and nobody has signed in yet. Otherwise it returns false and changes nothing. Such a call doesn't count as a wrong PIN.
The right PIN signs in and returns true. It also sets the card's count of wrong PINs back to 0.
A wrong PIN returns false and adds 1 to the card's count. The count stays with the card, even after an eject. The third wrong PIN in a row locks the card for good. Then the machine gives the card back.
balance returns -1 when nobody is signed in.
withdraw picks notes from the largest down. For each note, from 100 to 10, it takes as many as fit in what is still left. It never takes more than the box holds.
The answer is "denied" in three cases. Nobody is signed in. Or some of the amount is still left after the 10s. Or the amount is more than the balance. A denied withdrawal changes nothing.
Otherwise the notes leave the box, and the balance goes down by amount. The answer lists the notes used, largest first, such as "100x2 20x1". A note that isn't used isn't listed.
Taking the largest note first can fail even when smaller notes would do. Example 2 shows this.
Whoever signed in stays signed in until the eject. eject leaves the machine idle. With no card inside, it returns false.
The calls run in order on one new ATM. A call to addAccount or load writes nothing. A call to insert, enterPin or eject writes True or False. A call to balance writes the number. A call to withdraw writes its answer.
Example 1:
Input: NewATM() 1. addAccount(501, 1234, 300) 2. load(100, 5) 3. load(20, 5) 4. insert(501) 5. enterPin(1111) 6. enterPin(1234) 7. balance() 8. withdraw(140) 9. withdraw(200) 10. balance() 11. eject() 12. balance() Output: True False True 300 100x1 20x2 denied 160 True -1 Explanation: Call 5 uses a wrong PIN, so nobody signs in. Call 6 uses the right PIN. For 140, the machine takes one 100 first. The other 40 is two 20s. Now the balance is 160. Call 9 asks for 200. The box has the notes, but the balance is too low. After the eject, nobody is signed in. So call 12 returns -1.
Example 2:
Input: NewATM() 1. addAccount(7, 2580, 1000) 2. load(50, 1) 3. load(20, 3) 4. insert(7) 5. enterPin(2580) 6. withdraw(60) 7. withdraw(70) 8. withdraw(40) 9. withdraw(20) 10. balance() Output: True True denied 50x1 20x1 20x2 denied 890 Explanation: The box holds one 50 and three 20s. In call 6, the 50 goes first, and 10 is left. The box has no 10s, so call 6 is denied. Three 20s would make 60, but the machine never tries that. Call 7 takes the 50 and one 20. Call 8 takes the last two 20s. The box is empty now, so call 9 is denied. The balance is 1000 - 70 - 40 = 890.
Example 3:
Input: NewATM() 1. addAccount(42, 1000, 50) 2. withdraw(10) 3. enterPin(1000) 4. insert(42) 5. enterPin(9999) 6. enterPin(8888) 7. eject() 8. insert(42) 9. enterPin(7777) 10. eject() 11. insert(42) Output: denied False True False False True True False False False Explanation: Calls 2 and 3 come before any card is inside. They change nothing, and call 3 is not a wrong PIN. Calls 5 and 6 are wrong PINs. The card has 2 wrong PINs in a row now. The count stays with the card after the eject in call 7. Call 9 is the third wrong PIN in a row. The card is locked, and the machine gives it back. So call 10 finds no card inside. A locked card can't go back in, so call 11 returns False.
Constraints:
- A card number is a whole number from 1 to 1,000,000,000.
- Each card is added only once.
- A PIN is a whole number from 1000 to 9999.
- A starting balance is a whole number from 0 to 1,000,000.
noteis 100, 50, 20 or 10.countis from 1 to 10,000.amountis a multiple of 10, from 10 to 1,000,000.- At most 100,000 calls follow the constructor.
- At most 50,000 of them add accounts.
- Every number stays under 2,000,000,000.
Note:
Each call should run in O(1) time. Keep the accounts in a map from card to account. Searching a list of n accounts on each insert would take O(n) time.
Free plan: 30 tutor messages. PRO is unlimited.
