Coding Interview Prep: The Two-number sum problem.

Moeedlodhi
2 min readNov 9, 2023

Let’s make this a bit more useful.

I faced this recently. Yea sure, One could use ChatGPT to solve it for them but no harm in sharing my experience on how I solved this. So let’s get started.

The problem is straightforward and simple. We have an array of integers as shown below:

[1, 6, 5, 3, 4, 7, 3, -1, 4, 2]

And a target value which can be let’s say 12.

The job is to find any two numbers inside of the array that can add up to be equal to the target value. But do note that no single number can be used twice and that there will be at least one pair at all times inside of the array. If there are no two numbers, an empty array can be returned.

That being said, Let’s get started.

There are multiple ways in which this problem can be tackled, always multiple solutions out there. But the way I did it is like this.

Go through the array, and subtract each number one by one from the target value.

Find the result of each subtraction inside of the array. If it’s there, you have found your pair of integers which sum up to be equal to the target and there is no need to further go through the array.

So in our array

[1, 6, 5, 3, 4, 7, 3, -1, 4, 2]

The first integer is 1. We subtract 1 from 12 and what do we get? 10

--

--