# Coding Problems and Solutions

Programming | 03 October 2019

### Easy

##### problem 001

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. Can you do this in one pass?

problem_e001.pycode
1
2
3
4
5
6
7
8
9
10
11
12
def prob_e001(arr, k):
s = set()
for i in range(len(arr)):
temp = k - arr[i]
if temp in s:
print("Pair with sum "+ str(k) + " is (" + str(arr[i]) + ", " + str(temp) + ")")

import time
start_time = time.time()
prob_e001([1, -1, 5, 6], 7)
print("--- %s micro seconds ---" % ((time.time() - start_time)*1000000))

1
2
Pair with sum 7 is (6, 1)
--- 314.95094299316406 micro seconds ---


### Medium

##### problem 001

Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.

For example,

• If the input is [1, 2, 3, 4, 5], the expected output is [120, 60, 40, 30, 24]
• If the input is [3, 2, 1], the expected output is [2, 3, 6]
problem_m001.pycode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# best approach without using division
# time complexity  : O(n)
# space complexity : O(n)
def prob_m001(arr):
left  = [None] * len(arr)
right = [None] * len(arr)
res   = [None] * len(arr)

left, right[(len(arr))-1] = 1, 1

for i in range(1, len(arr)):
left[i] = arr[i-1] * left[i-1]

for i in range((len(arr)-2), -1, -1):
right[i] = arr[i+1] * right[i+1]

for i in range(len(arr)):
res[i] = left[i] * right[i]

return res

import time
start_time = time.time()
print(prob_m001([1, 2, 3, 4, 5]))
print("--- %s micro seconds ---" % ((time.time() - start_time)*1000000))

1
2
[120, 60, 40, 30, 24]
--- 437.73651123046875 micro seconds ---


### Hard

In case if you found something useful to add to this article or you found a bug in the code or would like to improve some points mentioned, feel free to write it down in the comments. Hope you found something useful here.