Practice
Solution
Step 1: Understand the problem constraints
The system must support fast posting and retrieving the top 10 recent tweets from a user's followees including themselves.Step 2: Evaluate approaches
Brute force scanning or sorting all tweets each time is inefficient. DP is not applicable here. Using linked lists per user and a max heap for merging recent tweets achieves O(1) posting and O(k log F) retrieval, where F is followees count.Final Answer:
Option B -> Option BQuick Check:
Linked list + hashmap + heap is the optimal pattern for this problem [OK]
- Thinking sorting all tweets each time is efficient
- Trying DP for a streaming feed problem
Solution
Step 1: Understand problem requirements
The data structure must efficiently support set, snap, and get operations with many snapshots and updates.Step 2: Evaluate approaches
Storing full snapshots (B) uses too much space and is slow for many snaps. A global hash map with linear search (C) makes get operations slow. Segment trees (D) are complex and not optimal here. Using a hash map per index with binary search (A) allows O(1) amortized set and snap, and O(log m) get, balancing time and space efficiently.Final Answer:
Option B -> Option BQuick Check:
Binary search per index enables fast retrieval [OK]
- Thinking full snapshots are efficient for many snaps
- Using linear search for get causes slow queries
Solution
Step 1: Identify data structure operations
Removal uses hash map to get a node reference in O(1) average time, then removes node from doubly linked list in O(1).Step 2: Clarify average vs worst-case
Hash map lookups are average O(1), but worst-case can degrade. Linked list removal is always O(1). So overall average is O(1).Final Answer:
Option D -> Option DQuick Check:
Hash map + linked list enables average O(1) removals [OK]
- Confusing average and worst-case complexities
- Assuming linked list removal is O(log n)
- Thinking index search is needed
put operation in the optimal LFU Cache implementation using a frequency map of doubly linked lists and hash maps?Solution
Step 1: Analyze data structures used
Hash maps provide O(1) access to keys and frequencies. Doubly linked lists allow O(1) insertion and deletion of keys within frequency groups.Step 2: Consider operations in put
Eviction and frequency updates involve O(1) operations on hash maps and linked lists. Frequency increments and min frequency updates are also O(1).Final Answer:
Option D -> Option DQuick Check:
Optimal LFU cache achieves O(1) amortized put [OK]
- Assuming O(n) due to scanning keys
- Thinking frequency updates cost O(log n)
- Confusing distinct frequencies with n
addRange method for the RangeModule. Which line contains the subtle bug that causes incorrect interval merging?
def addRange(self, left: int, right: int) -> None:
i = bisect_left(self.starts, left)
j = bisect_right(self.starts, right)
if i != 0 and self.intervals[self.starts[i-1]] > left:
i -= 1
left = min(left, self.starts[i])
if j != 0:
right = max(right, self.intervals[self.starts[j-1]])
for k in self.starts[i:j]:
del self.intervals[k]
self.starts[i:j] = [left]
self.intervals[left] = right
Solution
Step 1: Analyze the condition for merging overlapping intervals
The condition uses > instead of >=, so intervals that exactly touch at the boundary are not merged.Step 2: Consequence of the bug
This causes fragmented intervals and incorrect query results because adjacent intervals are not merged properly.Final Answer:
Option A -> Option AQuick Check:
Using > instead of >= misses boundary merges [OK]
- Using strict inequality instead of inclusive for merging intervals
