#540 Implementation of ATM Cash Withdrawal

Companies:
Wildberries
Themes:
KotlinOOPPractice

The Hardware interface for interacting with the ATM hardware is given:

SQL
/**
 * Interface for interacting with the ATM hardware.
 */
interface Hardware {
    /**
     * Returns an array containing the number of banknotes for denominations
     * 50, 100, 500, 1000, and 5000.
     * The method is slow and creates noise.
     *
     * @return an array where every element corresponds to the number of banknotes
     * of a specific denomination.
     * For example, [10, 20, 30, 40, 50] means:
     * - 10 banknotes of 50 rubles
     * - 20 banknotes of 100 rubles
     * - 30 banknotes of 500 rubles
     * - 40 banknotes of 1000 rubles
     * - 50 banknotes of 5000 rubles
     */
    fun billsCounts(): IntArray

    /**
     * Loads the specified banknotes into the cash-dispensing box.
     *
     * @param billsCounts an array containing the number of banknotes for
     * denominations [50, 100, 500, 1000, 5000].
     * For example, [0, 1, 0, 2, 0] means:
     * - 0 banknotes of 50 rubles
     * - 1 banknote of 100 rubles
     * - 0 banknotes of 500 rubles
     * - 2 banknotes of 1000 rubles
     * - 0 banknotes of 5000 rubles
     */
    fun giveBills(billsCounts: IntArray)
}

Implement the MyATM class responsible for ATM business logic.

Java
/**
 * Class implementing ATM business logic.
 * Write the code here.
 */
class MyATM {
    /**
     * ATM hardware.
     */
    var hardware: Hardware? = null
    val nominals = [50, 100, 500, 1000, 5000]

    fun calcTotal(): Int

    fun giveMoney(req: OperationRequest): OperationResponse

    data class OperationRequest(val sum: Int)

    data class OperationResponse(val status: OperationStatus)
}

The calcTotal() method must return the total amount of money currently stored in the ATM.

The giveMoney(req) method must attempt to dispense the requested amount. If the amount can be dispensed using the available banknotes, call hardware.giveBills(...) and return a successful status. If the amount cannot be dispensed, return the corresponding error status.

Keep in mind that hardware.billsCounts() is slow and noisy, so it must not be called unnecessarily.

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in