Practice
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
Solution
Step 1: Insert 1
Low heap empty, push -1 to low; low_size=1, high_size=0.Step 2: Insert 5
5 > -low[0] (which is 1), push 5 to high; low_size=1, high_size=1; heaps balanced.Step 3: Insert 3
3 <= 5 but > 1, push 3 to high; high_size=2, low_size=1; balance heaps by moving smallest from high (3) to low (-3); low_size=2, high_size=1.Step 4: Find median
low_size > high_size, median is -low[0] = 3.0.Step 5: Re-examine median calculation
After balancing, low heap has [-3, -1], high heap has [5]. Median is top of low heap = 3.0, but the question asks for median after adding 1,5,3 in order, so median is 3.0.Final Answer:
Option B -> Option BQuick Check:
Median after [1,5,3] is 3.0 [OK]
- Forgetting to balance heaps after insertion
- Returning wrong heap top for median
def inc(self, key: str) -> None:
if key not in self.key_to_bucket:
if 1 not in self.count_to_bucket:
new_bucket = Bucket(1)
self._insert_bucket_after(new_bucket, self.head)
self.count_to_bucket[1].keys.add(key)
self.key_to_bucket[key] = self.count_to_bucket[1]
else:
curr_bucket = self.key_to_bucket[key]
next_count = curr_bucket.count + 1
if next_count not in self.count_to_bucket:
new_bucket = Bucket(next_count)
self._insert_bucket_after(new_bucket, curr_bucket)
self.count_to_bucket[next_count].keys.add(key)
self.key_to_bucket[key] = self.count_to_bucket[next_count]
# BUG: Missing removal of key from old bucket
# curr_bucket.keys.remove(key)
if len(curr_bucket.keys) == 0:
self._remove_bucket(curr_bucket)Solution
Step 1: Identify key movement between buckets
When incrementing, key must be removed from old bucket's keys set.Step 2: Locate missing removal
The commented out line that removes the key from old bucket is missing, causing key to appear in multiple buckets.Final Answer:
Option A -> Option AQuick Check:
Without removal, max/min queries become incorrect due to duplicate keys [OK]
- Forgetting to remove key from old bucket after increment/decrement
Solution
Step 1: Identify the role of curr.child
After splicing the child list, curr.child must be set to None to avoid revisiting the child.Step 2: Recognize missing pointer update
The code does not set curr.child = None, causing infinite loops or incorrect traversal.Final Answer:
Option A -> Option AQuick Check:
Not clearing child pointer leads to repeated processing [OK]
- Forgetting to clear child pointer after splicing
- Incorrectly updating prev pointers instead
def remove(self, val: int) -> bool:
if val not in self.idx_map or not self.idx_map[val]:
return false
remove_idx = self.idx_map[val].pop()
last_val = self.nums[-1]
self.nums[remove_idx] = last_val
self.idx_map[last_val].add(remove_idx)
# BUG: missing discard of old index for last_val
self.nums.pop()
if not self.idx_map[val]:
del self.idx_map[val]
return true
Solution
Step 1: Understand index updates during removal
When swapping last_val into remove_idx, we must update idx_map[last_val] by adding remove_idx and discarding the old index (len(nums)-1).Step 2: Identify missing discard
The code adds remove_idx to idx_map[last_val] but does not discard the old index, causing stale indices and incorrect removals later.Final Answer:
Option A -> Option AQuick Check:
Missing discard leads to incorrect index tracking [OK]
- Forgetting to discard old index after swap
- Removing wrong element from idx_map
- Incorrect pop from idx_map[val]
