Question 61
HardWhat is the output of this code?
from collections import defaultdict
a = [1, , , , , ]
k =
cnt, s, seen = , , defaultdict()
seen[] =
x a:
s += x
cnt += seen[s - k]
seen[s] +=
(cnt)
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?
from collections import defaultdict
a = [1, , , , , ]
k =
cnt, s, seen = , , defaultdict()
seen[] =
x a:
s += x
cnt += seen[s - k]
seen[s] +=
(cnt)
2
5
4
3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def longest(a):
first, s, best = {0: -1}, 0, 0
for i, x in enumerate(a):
s += 1 if x == 1 else -1
if s in first:
best = max(best, i - first[s])
else:
first[s] = i
return best
print(longest([0, 1, 0, 0, 1, 1, 0]))
4
7
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def is_iso(s, t):
return len(set(zip(s, t))) == len(set(s)) == len(set(t))
print(is_iso("egg", "add"), is_iso("foo", "bar"), is_iso("paper", "title"))
True True True
True False True
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
d = {}
for w in ["listen", "silent", "enlist", "google", "gogole", "cat"]:
d.setdefault("".join(sorted(w)), []).append(w)
print(max(len(v) for v in d.values()), len(d))
3 3
3 6
Sign up free to see the answer and a step-by-step explanation.
Keys 14, 25, 36 and 47 are inserted into a table of size 11 with double hashing: h₁(k) = k mod 11 and step h₂(k) = 7 − (k mod 7). Where does 47 go?
Slot 0
Slot 5
Slot 8
Slot 3
Sign up free to see the answer and a step-by-step explanation.
With separate chaining and a load factor of 2, how many key comparisons does an unsuccessful search need on average?
1
3
2
0.5
Sign up free to see the answer and a step-by-step explanation.
With linear probing at load factor α, why do searches slow down sharply as α approaches 1?
Keys are deleted
The hash function changes
Chains get longer
Clusters merge into long runs that every probe must cross
Sign up free to see the answer and a step-by-step explanation.
Why do production hash maps use a random seed in their string hash?
To compress keys
To sort keys
To make hashing faster
To stop attackers from crafting many keys that collide (hash-flooding denial of service)
Sign up free to see the answer and a step-by-step explanation.
Which property does a cryptographic hash have that a hash-table hash usually doesn't need?
It is infeasible to find two inputs with the same hash
It is fast
It returns an integer
It is deterministic
Sign up free to see the answer and a step-by-step explanation.
What does cuckoo hashing guarantee for lookups?
Nothing
O(n)
Worst-case O(1): a key can only be in one of two places
O(log n)
Sign up free to see the answer and a step-by-step explanation.
6
2
False False True
True False False
2 3
3 2