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
Solution
Step 1: Analyze addNum complexity
Adding a number involves pushing to a heap and balancing heaps, each O(log n) operations.Step 2: Analyze findMedian complexity
Median is retrieved from the top of heaps without modification, O(1) time.Final Answer:
Option C -> Option CQuick Check:
Insertion is O(log n), median retrieval O(1) [OK]
- Assuming sorting each query is O(log n)
- Confusing addNum with findMedian complexity
import bisect
class SnapshotArray:
def __init__(self, length: int):
self.snap_id = 0
self.data = [[(-1, 0)] for _ in range(length)]
def set(self, index: int, val: int) -> None:
# Bug here
self.data[index].append((self.snap_id, val))
def snap(self) -> int:
self.snap_id += 1
return self.snap_id - 1
def get(self, index: int, snap_id: int) -> int:
arr = self.data[index]
i = bisect.bisect_right(arr, (snap_id, float('inf'))) - 1
return arr[i][1]
Solution
Step 1: Analyze set() method
The set method always appends (snap_id, val) without checking if the last entry has the same snap_id, causing duplicate entries for the same snap_id.Step 2: Consequence on get()
Duplicate entries for the same snap_id break binary search assumptions and can cause get to return incorrect values.Final Answer:
Option C -> Option CQuick Check:
Correct code replaces last entry if snap_id matches [OK]
- Appending blindly causes duplicates
- Misunderstanding initialization defaults
Solution
Step 1: Understand tweet reuse impact
Allowing duplicate tweetIds means multiple posts with same id but different times must be treated as distinct tweets.Step 2: Adjust data structures
Each post creates a new TweetNode with unique timestamp; heap and linked lists handle duplicates naturally by time ordering.Final Answer:
Option A -> Option AQuick Check:
Multiple nodes with same tweetId but different times must be stored separately [OK]
- Trying to deduplicate tweets in feed incorrectly
- Rejecting duplicate posts unnecessarily
