JavaScript Coding Test Course, Calculate ATM Withdrawal Time

Common problems that appear in programming interviews or coding tests are time calculation problems in various situations. In this post, we will discuss an algorithm problem that calculates the withdrawal time at an ATM. Assuming multiple users are using the ATM at the same time, we will write an algorithm to calculate the expected waiting time for each user.

Problem Description

Let’s assume there is one ATM machine. There are several users waiting in line for this ATM. Each user can use the ATM at a specific time, and the time required to use the ATM can vary from person to person.

The given input is an array of users’ withdrawal times. The index of the array represents the user’s order, and the value stored at that index indicates the time taken by that user to use the ATM in seconds. For example, if an array of [5, 3, 8] is provided, the first user uses the ATM for 5 seconds, the second user for 3 seconds, and the third user for 8 seconds.

Input

[5, 3, 8]

Output

Array of waiting times for each user: [0, 5, 8]

In the above example, the first user has a waiting time of 0 seconds, so they can use it immediately. The second user can use the ATM after the first user’s withdrawal is finished, so their waiting time is 5 seconds. The third user can use the ATM after the second user’s waiting time is over, making their waiting time 8 seconds.

Problem Solving Process

Step 1: Understanding the Problem

To understand the problem, the following must be clarified:

  • Each user using the ATM must wait until the previous user’s withdrawal is completed.
  • To calculate waiting times, cumulative time must be tracked.
  • The waiting times should be calculated in the order of withdrawal, and the result should be returned as an array.

Step 2: Designing the Algorithm

To solve this problem, the following algorithm can be used:

  1. Create an empty array to store each user’s waiting times.
  2. Initialize a variable totalTime to 0. This will be used to store cumulative waiting time.
  3. Loop through each user’s withdrawal time to calculate the waiting times:
    • The current user’s waiting time is set to totalTime.
    • Add the current user’s withdrawal time to totalTime.
  4. Return the waiting time array.

Step 3: Code Implementation

Now, let’s implement the algorithm in JavaScript code.

function calculateWaitTimes(atmTimes) {
    let waitTimes = [];
    let totalTime = 0;

    for (let i = 0; i < atmTimes.length; i++) {
        waitTimes[i] = totalTime; // Current user's waiting time
        totalTime += atmTimes[i]; // Update cumulative time
    }

    return waitTimes;
}

// Example execution
const inputTimes = [5, 3, 8];
const outputWaitTimes = calculateWaitTimes(inputTimes);
console.log(outputWaitTimes); // [0, 5, 8]

Testing and Validation

After implementing the function, let’s validate it using various test cases to ensure it produces the correct results.

console.log(calculateWaitTimes([0, 0, 0]));       // [0, 0, 0]
console.log(calculateWaitTimes([1, 2, 3]));       // [0, 1, 3]
console.log(calculateWaitTimes([10, 20, 30]));    // [0, 10, 30]
console.log(calculateWaitTimes([5]));              // [0]
console.log(calculateWaitTimes([]));               // []

Complexity Analysis

The time complexity of this algorithm is O(n). It increases in proportion to the number of users since it traverses the array once. The space complexity is also O(n), as an array is needed to store the waiting times.

Conclusion

In this post, we examined the problem of calculating withdrawal times at an ATM. This problem can serve as a useful exercise for mastering the basics of algorithm design and time calculation. Through the implementation process in JavaScript, I hope you have learned a more accessible approach to similar problems encountered in actual coding tests. It is hoped that this problem helps you understand the fundamental approaches to algorithms and time complexity and enhances your problem-solving skills.