Daily Coding Problem – Given a list of numbers and a number k, return whether any two numbers from the list add up to k.

Problem

Daily Coding Problem sends daily coding problem sample when you subscribe to their service. This service is very useful to practice your coding ability and think through the logic you will be using to solve the problem.

I received the following problem for the 11/03/2018 coding problem.

This problem was asked by Google.

Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.

Bonus: Can you do this in one pass?

Context

Based on the above given problem, basically, you need to find out if there are two values can sum to ‘k’ (which in this case “17”).

Solution

Below how I approached the problem.

Let me show the illustration how I thought of solving this problem.

There are items in the list. Iterate from first item then add the next item and check if there is a match to ‘k’. If there is no match, keep going. once it went through the first iteration, pop() the first item and refresh the list. Then, repeat the same step until you exhaust all the items in the list.

So you can see from above illustration, if I have [10, 15, 3, 7], I will try to add 10 + 15 first, and the sum is 25, which is not equal to 17, so move on to next one which is 10 + 3 and the sum is 13 which is not equal to 17. And so on…

If at some point, it matches the sum to 17, it will exist the recursion. If no match, remove (pop) the fist item in the list and try it again until it may or may not match the sum to 17.

Code Example

So below is the code I came up with:

# given values
l = [10, 15, 3, 7]
k = 17

# will be used for recursion
l_len = len(l)

# do the work
def compute(a_list, k):
    
    # loop through the list
    for i in range(l_len):

        a_list_len = len(a_list)
        base_num = a_list[0]
        sum_num = 0

        # pop the first item in the list
        a_list.pop(0)

        # refresh the list without the first one
        a_list_len = len(a_list)

        for j in range(a_list_len):
            sum_num = base_num + a_list[j]
            # print(base_num, a_list, a_list_len, sum_num)

            if sum_num == k:
                # return the values used to match the k value
                return base_num, a_list[j]
    
    # return False if nothing matches.
    return False

r = compute(l, k)

if not r:
    print("there was no match!")
else:
    print(f'there was a match with k({k}), {r[0]} + {r[1]}')

Step 1

you need to figure out how many items in the list. In the given example, it has 4 items (but you should consider number of items can be more or less). So using Python built-in function len(), you can find out the number of items in the list. We will use this value to iterate inside the list.

Step 2

Next, keep the first item in the variable (base_num) then pop the first item in the list. Now refresh the set of items.

Step 3

Use the refreshed list and iterate within. While iterating, check if the sum of base_num and a_list[j] match the k value.  If yes, return True (in the code above, returning the used values)

Step 4

If there is no match, keep iterating until it hits the condition or not.

Step 5

Output either there was a match or not.

Code Download

You may download the sample code here if you wan to try it out by yourself. Let me know how you approach this problem.

Feel free to share this post!

Scroll to Top