Adam Hunter

Made with in NYC

Two Sum

One of the most popular interview algorithms in JavaScript and Python

12.9.22

For this blog post I wanted to break down one of the most commonly asked algorithms in coding interviews. Companies like Amazon, Apple, Google, Microsoft, Adobe, Twitch, Spotify, Facebook, Uber and Tesla are reportedly still asking candidates to solve this algorithm. The famed LeetCode has thousands of these types of questions and Two Sum is the very first one they throw at you.

There are basically four different ways to solve this problem. There is the brute force method, the two pointer method and then two different ways using hash tables (objects). I just want to just break down the first approach. The other approaches are technically better because of their time and space complexities but if you are new to coding or algorithms, solving this with any complexity is a huge win. Since most courses and resources are so focused on being the best, I wanted to write the blog post that I wish I found years ago when I first started getting into these.

Two Sum. Let’s get into it.
Given an array of integers (nums) and an integer (target), return indices of the two numbers such that they add up to the target. You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]

In plain English, you get a list (array) of numbers and target number and have to find which two numbers in the list add up to the target. The only tricky part is that they ask you to return the indices and not the actual numbers but we will worry about that in a few minutes.

I’ll show this in both JavaScript and Python but the approach is exactly the same in both languages. Off the bat, we see that we are expected to return a new list with our answers inside. The very first thing we code will be creating an empty list to put our answers inside and then return that list. We need to compare the numbers of the given list. To do so, we can use two for loops. Let’s open up the list with the first for loop.


First JavaScript. Now Python.

Using the examples given with the directions, these will both print the same data to the terminal console.

We have our new empty list for our answers and we now have access to all of the numbers in the given array. Inside of our first for loops we are using i to access the indices of each number and conveniently we access the numbers using the same syntax for getting the number at i index. For loops. Wild. Still with me? Let’s open up that second for loop so we can compare the numbers. Best practice is to use j for the second index. J will run one index ahead of i.



Cool, now we have both for loops running and can now write some logic to compare the numbers. This is really the core of the entire problem. We want to see if one number (nums[i]) in the array plus another number (nums[j]) in the array equals the target number. We can accomplish this with an if else statement.


Now our algorithm is checking to see which numbers in the array sum to the target. Pretty cool. Even if you are really bad at math and have trouble figuring out if 2 + 7 = 9, don’t worry because the computer is now checking for us.
Now that our algorithm is finding out which numbers we need, we just need to fill that empty array from the beginning with our answers. Also remember from the beginning, the directions are asking for us to return the indices of the number and not the actual number. Instead of nums[i] and nums[j], we just need to put i and j into our answers array. With JavaScript, we can use the .push() method and with Python we can use .append(). We also do not need the else statement.




Nice! We did it. Nothing too fancy, nested for loops and an if statement does the trick. I really hope breaking this down step by step and visualizing what each piece does helps somebody new on their coding journey.

Adamadam hi

Adam