Practice
Solution
Step 1: Understand the problem requirements
The system must support frequent check-ins, check-outs, and quick average time queries between stations.Step 2: Evaluate approaches
Storing all trips (B) leads to slow queries. Dynamic programming (C) is not applicable as routes are dynamic and not fixed. Priority queues (D) do not help compute averages efficiently. Hash maps with running totals (A) allow O(1) updates and queries.Final Answer:
Option A -> Option AQuick Check:
Hash maps maintain running sums and counts for O(1) average time queries [OK]
- Thinking storing all trips is efficient for queries
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: Understand cycle implications
Random pointer cycles cause infinite loops if naive traversal is used without tracking visited nodes.Step 2: Identify safe approach
Recursive copying with memoization tracks visited nodes, preventing infinite recursion and duplicate copies.Final Answer:
Option B -> Option BQuick Check:
Memoization ensures each node is copied once even with cycles [OK]
- Assuming weaving approach handles cycles safely
- Ignoring cycles and causing infinite loops
- Removing random pointers loses information
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
