Question 51
MediumWhich data structures combine to build an LRU cache with O(1) get and put?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
Which data structures combine to build an LRU cache with O(1) get and put?
A BST and a queue
A hash map and a doubly linked list
Two stacks
A heap and an array
Sign up free to see the answer and a step-by-step explanation.
What is a Bloom filter's guarantee?
It may say 'possibly present' for an absent key, but never 'absent' for a present one
It never makes mistakes
It stores the keys themselves
It sorts keys
Sign up free to see the answer and a step-by-step explanation.
In Rabin–Karp, why is a rolling hash used?
The hash of the next window is computed in O(1) from the previous one
It sorts the text
It avoids all collisions
It needs no modulus
Sign up free to see the answer and a step-by-step explanation.
What is the expected time to build a frequency map of n words with a hash map?
O(n²)
O(n)
O(n log n)
O(log n)
Sign up free to see the answer and a step-by-step explanation.
A hash table uses double hashing with h₁(k) = k mod 7 and h₂(k) = 5 − (k mod 5). Slot 3 is occupied when key 10 is inserted. Which slot is tried next?
Slot 4
Slot 1
Slot 0
Slot 6
Sign up free to see the answer and a step-by-step explanation.
What is the expected number of probes for an unsuccessful search with separate chaining at load factor α?
α²
1 + α
log α
1/(1 − α)
Sign up free to see the answer and a step-by-step explanation.
Why does a HashMap key class in Java need consistent equals() and hashCode() methods?
hashCode() is never used
Equal objects must have equal hash codes, or lookups may search the wrong bucket
To sort the map
To make keys immutable
Sign up free to see the answer and a step-by-step explanation.
What happens to a Java HashMap when its size exceeds capacity × load factor (0.75 by default)?
It resizes (doubles capacity) and rehashes all entries
It becomes a TreeMap
It stops accepting keys
It throws an exception
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
from collections import defaultdict
g = defaultdict(list)
for w in ["eat", "tea", "tan", "ate", "nat", "bat"]:
g["".join(sorted(w))].append(w)
print(list(g.values()))
[['eat'], ['tea'], ['tan'], ['ate'], ['nat'], ['bat']]
[['bat'], ['eat', 'tea', 'ate'], ['nat', 'tan']]
Sign up free to see the answer and a step-by-step explanation.
What is consistent hashing mainly used for?
Sorting data
Distributing keys across servers so that adding or removing a server moves only a small fraction of keys
Encrypting passwords
Compressing files
Sign up free to see the answer and a step-by-step explanation.
[['eat', 'tea', 'tan', 'ate', 'nat', 'bat']]
[['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']]