Round 1: Programming Test
Input: {3, 4, 7, 1, 2, 9, 8} Output: (3, 8) and (4, 7) Explanation: 3+8 = 4+7 Input: {65, 30, 7, 90, 1, 9, 8}; Output: No pairs found
def getpairs(elements): d=dict() l=len(elements) for i in range(l-1): for j in range(i+1,l): s=elements[i]+elements[j] if s not in d: d[s]=(elements[i],elements[j]) else: print("pairs are %s and (%d,%d)"%(str(d[s]),elements[i],elements[j])) getpairs([3, 4, 7, 1, 2, 9, 8])
Input: 90 Output: 5 Input: 84 Output: 7
def findMaxPrime(n): while n % 2 == 0: mp = 2 n = n / 2 for i in range(3, int(n), 2): while n % i == 0: mp = i n = n / i if n > 2: mp = n return int(mp) print(findMaxPrime(84))
Input: a = 9, b = 25 Output: 3 The three squares in given range are 9, 16 and 25
import math def getNumberOfSqaures(a, b): return (math.floor(math.sqrt(b)) - math.ceil(math.sqrt(a)) + 1) print("getNumberOfSqaures :", getNumberOfSqaures(9, 25))
def makeZero(grid): row = len(grid) col = len(grid[0]) r, c = list(), list() for i in range(row): for j in range(col): if grid[i][j] == 0: r.append(i) c.append(j) for i in range(row): for j in range(col): if i in r or j in c: grid[i][j] = 0 grid = [ [1, 1, 1], [1, 0, 1], [1, 1, 1] ] print("input -> ", grid) makeZero(grid) print("Output -> ", grid)
Round 2: Video call interview
Let's have a string input = "hello world 55 test 77" then print the sum of digits of numbers in pair. output = (10,14). Don't use regular expression.
Solutiondef getSum(string): """Without using regular expression""" k = 0 found = False s = 0 d = list() for i in string + " ": if i.strip() in [str(j) for j in range(10)]: s = s + int(i) found = True k = k + 1 else: found = False if k > 0 and not found: d.append(s) s = 0 k = 0 print(tuple(d)) getSum("hello world 55 test 77")
import time import multiprocessing def raise_time_out(): p = multiprocessing.Process(target=testtime, args=[10]) p.start() p.join(5) if p.is_alive(): print('Process taking more than 5 seconds..Lets kill it') p.terminate() p.join() if __name__ == "__main__": start = time.perf_counter() raise_time_out() end = time.perf_counter() print(f'Processing finished in {end - start} seconds')
This article is contributed by Nupur. If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.