-
Notifications
You must be signed in to change notification settings - Fork 13
/
interviewQuestionsBigO.py
107 lines (82 loc) · 1.94 KB
/
interviewQuestionsBigO.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
################ Interview Questions #############
#Question1
def foo(array):
sum = 0
product = 1
for i in array:
sum += i
for i in array:
product *= i
print("Sum = "+str(sum)+", Product = "+str(product))
ar1 = [1,2,3,4]
foo(ar1)
#Question2
def printPairs(array):
for i in array:
for j in array:
print(str(i)+","+str(j))
#Question3
def printUnorderedPairs(array):
for i in range(0,len(array)):
for j in range(i+1,len(array)):
print(array[i] + "," + array[j])
#Question4
def printUnorderedPairs(arrayA, arrayB):
for i in range(len(arrayA)):
for j in range(len(arrayB)):
if arrayA[i] < arrayB[j]:
print(str(arrayA[i]) + "," + str(arrayB[j]))
arrayA = [1,2,3,4,5]
arrayB = [2,6,7,8]
#Question5
def printUnorderedPairs(arrayA, arrayB):
for i in range(len(arrayA)):
for j in range(len(arrayB)):
for k in range(0,100000):
print(str(arrayA[i]) + "," + str(arrayB[j]))
# printUnorderedPairss(arrayA,arrayB)
#Question6
def reverse(array):
for i in range(0,int(len(array)/2)):
other = len(array)-i-1
temp = array[i]
array[i] = array[other]
array[other] = temp
print(array)
reverse(arrayA)
#Question8
def factorial(n):
if n < 0:
return -1
elif n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(3))
#Question9
def allFib(n):
for i in range(n):
print(str(i)+":, " + str(fib(i)))
def fib(n):
if n <= 0:
return 0
elif n == 1:
return 1
return fib(n-1) + fib(n-2)
allFib(4)
#Question10
def powersOf2(n):
# print("n:"+str(n))
if n < 1:
return 0
elif n == 1:
print(1)
return 1
else:
prev = powersOf2(int(n/2))
# print("prev:"+str(prev))
print(prev)
curr = prev*2
print(curr)
return curr
powersOf2(50)