Question 31
MediumWhat is the output of this code?
nums = [100, 4, 200, , , ]
s = (nums)
best =
x s:
x - s:
y = x
y + s:
y +=
best = (best, y - x + )
(best)
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the output of this code?
nums = [100, 4, 200, , , ]
s = (nums)
best =
x s:
x - s:
y = x
y + s:
y +=
best = (best, y - x + )
(best)
5
2
4
3
Sign up free to see the answer and a step-by-step explanation.
Why should hash tables usually have a prime number of slots when using h(k) = k mod m?
It reduces memory use
It prevents all collisions
It spreads keys with common patterns (such as multiples of 10) more evenly
Prime numbers are faster to divide by
Sign up free to see the answer and a step-by-step explanation.
What is the worst-case time complexity of a hash table lookup?
O(log n)
O(n²)
O(1)
O(n)
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
from collections import defaultdict
nums, k = [1, 2, 1, 2], 3
count, s = 0, 0
seen = defaultdict(int)
seen[0] = 1
for x in nums:
s += x
count += seen[s - k]
seen[s] += 1
print(count)
4
3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
nums, target = [2, 7, 11, 15], 18
seen = {}
for i, x in enumerate(nums):
if target - x in seen:
print(seen[target - x], i)
break
seen[x] = i
2 1
0 3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
from collections import defaultdict
words = ["eat", "tea", "tan", "ate", "nat", "bat"]
g = defaultdict(list)
for w in words:
g["".join(sorted(w))].append(w)
print(sorted(len(v) for v in g.values()))
[1, 1, 4]
[1, 2, 3]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [100, 4, 200, 1, 3, 2]
s = set(a)
best = 0
for x in s:
if x - 1 not in s:
y = x
while y + 1 in s:
y += 1
best = max(best, y - x + 1)
print(best)
2
6
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [1, 1, 1]
k = 2
count, s, seen = 0, 0, {0: 1}
for x in a:
s += x
count += seen.get(s - k, 0)
seen[s] = seen.get(s, 0) + 1
print(count)
1
2
Sign up free to see the answer and a step-by-step explanation.
Keys 10, 17, 24 and 3 are inserted in that order into a table of size 7 with h(k) = k mod 7 and linear probing. In which slot does 3 end up?
3
5
4
6
Sign up free to see the answer and a step-by-step explanation.
Keys 10, 17 and 24 are inserted into a table of size 7 with h(k) = k mod 7 and quadratic probing (h + i²). Where does 24 go?
Slot 4
Slot 5
Slot 6
Slot 0
Sign up free to see the answer and a step-by-step explanation.
2
1
1 2
0 1
[2, 2, 2]
[3, 3]
4
3
0
3