Passive Air Cooling System and Solar Water Heater.ppt
Coding test review2
1.
2. Facebook – Coding Interview (20mins)
Q.
You have a sorted array of integers. Write a
function that returns a sorted array containing
the squares of those integers.
Examples)
[-7, -3, -1, 4, 8, 12] -> [1, 9, 16, 49, 64, 144]
4. 𝑂(𝑛) 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛
class Solution:
def sortedsquared(self, arrays):
ans = [0]*len(arrays)
count = 0
while len(arrays) != 0:
if abs(arrays[0]) >= abs(arrays[-1]):
ans[-count-1] = arrays.pop(0)**2
else:
ans[-count-1] = arrays.pop(-1) ** 2
count += 1
return ans
5. Google – Coding Interview (15mins)
Q.
You have a two integer arrays, a and b, and an integer
target value v. Determine whether there is a pair of
numbers, where one number is taken from a and the other
from b, that can be added together to get a sum of v.
Return True if such a pair exists, otherwise return False.
Examples)
a = [0, 0, -5]
b = [-10, 40, -3, 9]
v = -8
True
7. 𝑂(𝑛) 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛
class Solution:
def sumofTwo(self, a, b, v):
d = {}
for a_num in a:
d[v - a_num] = True
for b_num in b:
if b_num in d:
return True
return False