JavaScript Coding Test Course, Preparing for Resignation

To prepare for a resignation in a rational and effective manner, various skills and knowledge are required. In particular, proficiency in programming languages such as JavaScript plays a crucial role in preparing for coding tests. In this post, I will introduce an algorithm problem using JavaScript and explain the process of solving it in detail.

Problem Description

Problem: Sum of Two Numbers

Given an integer array nums and an integer target, return the indices of the two numbers in nums such that their sum equals target.

It is assumed that there is exactly one solution for each input, and you may not use the same element twice. The returned value should be the indices of the two numbers.

Example Input:

nums = [2, 7, 11, 15]

target = 9

Example Output:

[0, 1]

This example shows that nums[0] + nums[1] = 2 + 7 = 9.

Problem Solving Strategy

There are various methods to approach this problem. Here are some representative approaches:

  • My Approach: Using a double loop to compare two numbers
  • Approach using hashmap: Allows quick lookup when necessary

1. Approach Using Double Loop

The most intuitive way is to use a double loop to check all combinations of two numbers. We validate whether we reach the target sum by checking every combination of numbers. However, this method has a time complexity of O(n2), which can degrade performance.

function twoSum(nums, target) {
    for (let i = 0; i < nums.length; i++) {
        for (let j = i + 1; j < nums.length; j++) {
            if (nums[i] + nums[j] === target) {
                return [i, j];
            }
        }
    }
}

// Example execution
const nums = [2, 7, 11, 15];
const target = 9;
console.log(twoSum(nums, target)); // [0, 1]

2. Approach Using Hashmap

A more efficient method is to use a hashmap. As we iterate through the numbers, we store each number’s value as a key and its index as a value. Then, we compute the required difference for each value and look up this difference in the hashmap, allowing us to solve the problem in linear time complexity O(n).

function twoSum(nums, target) {
    const numMap = new Map();
    
    for (let i = 0; i < nums.length; i++) {
        const complement = target - nums[i];
        if (numMap.has(complement)) {
            return [numMap.get(complement), i];
        }
        numMap.set(nums[i], i);
    }
}

// Example execution
const nums = [2, 7, 11, 15];
const target = 9;
console.log(twoSum(nums, target)); // [0, 1]

Time Complexity

The first method has a time complexity of O(n2), while the second method has a time complexity of O(n). Thus, the second method is much more efficient. The second method has a space complexity of O(n) as it makes use of an additional hashmap.

Conclusion

This problem allowed me to improve my coding skills in JavaScript and try various approaches. In particular, the problem-solving method utilizing a hashmap can be very useful in coding tests.

As you prepare for resigning, you will need to prepare for coding tests. I hope to enhance my algorithm problem-solving abilities through repeated practice and increase my application skills in actual coding tests. Please look forward to the next post where I will tackle more complex problems!