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
Node1(val=1) -> Node2(val=2) -> Node3(val=3), where Node1.random = Node3, Node2.random = Node1, Node3.random = null.
After Step 2 (assigning random pointers), what is the value of
copy_curr.random.val when copy_curr points to the copied node of Node2?Solution
Step 1: Trace Step 1 (interleaving nodes)
Original list: 1 -> 2 -> 3 becomes 1 -> 1' -> 2 -> 2' -> 3 -> 3'.Step 2: Trace Step 2 (assign random pointers)
Node2.random = Node1, so copied Node2 (2') random = Node1.next = 1'. Thus, copy_curr.random.val = 1.Final Answer:
Option C -> Option CQuick Check:
copy_curr.random points to copied Node1 with val=1 [OK]
- Confusing original and copied nodes when assigning random pointers
- Off-by-one errors in stepping through interleaved list
- Assuming random pointer points to original node's val
mergeSkylines(left, right)?Solution
Step 1: Trace first iterations of the merge loop
Compare left[0]=(1,3) and right[0]=(2,4): 1 < 2, so x=1, h1=3, h2=0, max_h=3 -> merged=[[1,3]]Step 2: Next iterations and final merge
Next left[1]=(5,0), right[0]=(2,4): 2 < 5, x=2, h2=4, h1=3, max_h=4 -> merged=[[1,3],[2,4]]; then right[1]=(6,0) vs left[1]=(5,0): 5 < 6, x=5, h1=0, h2=4, max_h=4 (no change), then x=6, h2=0, max_h=0 -> merged=[[1,3],[2,4],[4,0],[5,0],[6,0]] after extending remaining points.Final Answer:
Option D -> Option DQuick Check:
Stepwise max height updates match merged skyline [OK]
- Off-by-one in indices
- Missing key points when heights change
def put(self, key: int, value: int) -> None:
if key in self.cache:
self._remove(self.cache[key])
node = Node(key, value)
self._add(node)
self.cache[key] = node
if len(self.cache) > self.capacity:
lru = self.head.next # Bug here
self._remove(lru)
del self.cache[lru.key]
Solution
Step 1: Understand eviction logic
Eviction must remove the least recently used node, which is at tail.prev, not head.next.Step 2: Identify bug
Code incorrectly evicts head.next, which is the most recently used node, causing wrong eviction.Final Answer:
Option A -> Option AQuick Check:
Evicting head.next breaks LRU policy, must evict tail.prev [OK]
- Evicting MRU instead of LRU
- Forgetting to delete from cache dict
- Not handling zero capacity
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
