Practice
inc and dec operations in the optimized AllOne data structure that uses doubly linked list buckets and hash maps, and why?Solution
Step 1: Identify operations involved in inc/dec
Operations include moving keys between buckets, inserting/removing buckets, and updating hash maps.Step 2: Analyze complexity of each operation
All pointer updates and hash map lookups/inserts/removals are O(1). No scanning or sorting is needed.Final Answer:
Option A -> Option AQuick Check:
All operations are pointer and hash map updates, no loops over keys [OK]
- Confusing bucket insertion as O(log n) or scanning keys as O(n)
def copyRandomList(head):
if not head:
return None
curr = head
# Step 1: Insert copied nodes
while curr:
new_node = Node(curr.val, curr.next)
curr.next = new_node
curr = new_node.next
# Step 2: Assign random pointers
curr = head
while curr:
if curr.random:
curr.next.random = curr.random # Bug here
curr = curr.next.next
# Step 3: Separate lists
curr = head
copy_head = head.next
copy_curr = copy_head
while curr:
curr.next = curr.next.next
if copy_curr.next:
copy_curr.next = copy_curr.next.next
curr = curr.next
copy_curr = copy_curr.next
return copy_headSolution
Step 1: Analyze random pointer assignment
The line assignscurr.next.random = curr.random, butcurr.randompoints to original nodes, not copied nodes.Step 2: Correct assignment
It should becurr.next.random = curr.random.nextto point to the copied node corresponding tocurr.random.Final Answer:
Option A -> Option AQuick Check:
Incorrect random pointer assignment breaks copied list correctness [OK]
- Assigning random pointer directly to original node
- Mixing original and copied nodes after weaving
- Forgetting to advance curr by two nodes
Solution
Step 1: Understand skiplist structure
Skiplist uses multiple levels with probabilistic promotion, reducing expected search path length to logarithmic in n.Step 2: Analyze average operation cost
Each operation traverses from top level down, with expected O(log n) steps due to geometric level distribution.Final Answer:
Option A -> Option AQuick Check:
Skiplist average complexity is well-known as O(log n) [OK]
- Assuming linear time due to linked list base
- Confusing traversal at each level as multiplicative
- Thinking arrays provide O(1) search in skiplist
Solution
Step 1: Identify the costly operation
Finding the tail of each child list requires traversing that child list fully.Step 2: Analyze nested traversal effect
Since this tail-finding happens for multiple nodes, total time can be quadratic in number of nodes.Final Answer:
Option D -> Option DQuick Check:
Repeated tail traversal causes O(n^2) time in worst case [OK]
- Assuming linear time because each node is visited once
- Confusing recursion stack space with iterative approach
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
