Practice
getMaxKey() after these operations?all_one = AllOne()
all_one.inc('apple')
all_one.inc('banana')
all_one.inc('apple')
all_one.dec('banana')
print(all_one.getMaxKey())Solution
Step 1: Trace increments and decrements
After inc('apple') twice, 'apple' count = 2. 'banana' incremented once then decremented once, so removed.Step 2: Check getMaxKey()
Max count bucket is 2 with 'apple'. So getMaxKey() returns 'apple'.Final Answer:
Option B -> Option BQuick Check:
Only 'apple' remains with max count 2 [OK]
- Forgetting to remove 'banana' after dec to zero count
getNewsFeed(1) after these operations?
twitter = Twitter()
twitter.postTweet(1, 5)
twitter.postTweet(2, 6)
twitter.followees[1] = {2}
print(twitter.getNewsFeed(1))
Assume getNewsFeed returns a list of tweetIds sorted from most recent to oldest.Solution
Step 1: Trace postTweet calls
User 1 posts tweet 5 at time=1, user 2 posts tweet 6 at time=2.Step 2: Trace getNewsFeed(1)
User 1 follows user 2, so heap contains tweets with times 1 (tweet 5) and 2 (tweet 6). Max heap pops tweet 6 first, then tweet 5.Final Answer:
Option D -> Option DQuick Check:
Heap orders tweets by descending time, so output is [6, 5] [OK]
- Returning tweets in posting order instead of time order
- Ignoring followees' tweets
Solution
Step 1: Understand the eviction criteria
The cache must evict the least frequently used key, and if multiple keys share the same frequency, evict the least recently used among them.Step 2: Identify data structures supporting O(1) operations
A frequency map with doubly linked lists allows grouping keys by frequency and maintaining recency order within each frequency group, enabling O(1) updates and eviction.Final Answer:
Option B -> Option BQuick Check:
Frequency map + doubly linked lists is the classic LFU O(1) approach [OK]
- Using sorting on eviction is too slow
- Min-heap adds O(log n) overhead
- Single list can't maintain frequency and recency efficiently
Solution
Step 1: Identify difference from correct code
The bug is that merged.append is called every iteration without checking if height changed, causing redundant points.Step 2: Understand impact of redundant points
Appending points even when max_h equals previous height leads to unnecessary key points, failing correctness checks.Final Answer:
Option A -> Option AQuick Check:
Correct code appends only if height changes [OK]
- Appending every iteration regardless of height change
- Incorrect pointer increments
Solution
Step 1: Understand impact of negative heights
Negative heights mean skyline can go below ground, so max height alone is insufficient to represent shape.Step 2: Modify merge logic accordingly
Need to track both max and min heights at each x to correctly merge skylines with negative and positive heights.Step 3: Evaluate other options
Using absolute values loses sign info; brute force is inefficient; no modification fails correctness.Final Answer:
Option B -> Option BQuick Check:
Tracking both max and min heights handles negative overlaps correctly [OK]
- Ignoring negative heights
- Using absolute values incorrectly
- Assuming no change needed
