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: Understand the problem constraints
The problem requires flattening a multilevel doubly linked list in-place without extra space.Step 2: Identify the approach that guarantees in-place flattening without extra space
Using a recursive depth-first traversal that returns the tail of each flattened child list allows splicing child lists correctly and efficiently without extra data structures.Step 3: Evaluate other options
Iterative splicing (Iteratively traverse the list, and whenever a node has a child, splice the child list in place by connecting the child's tail to the node's next, updating pointers accordingly.) can cause repeated tail searches leading to O(n^2) time and is less straightforward. Greedy appending (Apply a greedy approach that always appends child lists at the end of the main list after full traversal.) fails to maintain order. Dynamic programming (Use dynamic programming to store intermediate flattened sublists and merge them bottom-up.) is not applicable.Final Answer:
Option B -> Option BQuick Check:
Recursive DFS approach is standard and efficient for in-place flattening [OK]
- Assuming iterative splicing is always best despite complexity
- Appending child lists only at the end
- Using DP which is not applicable here
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
Solution
Step 1: Analyze push and pop operations
Each push and pop is O(1) amortized, as they add or remove one element.Step 2: Analyze increment operation
Increment only updates inc array at one index, O(1) per call. Propagation happens lazily during pop, each element's increment propagated once.Final Answer:
Option D -> Option DQuick Check:
All n operations combined run in O(n) total time [OK]
- Assuming increment is O(k) per call
- Confusing total time with per-operation time
Solution
Step 1: Understand weighted random requirements
getRandomWeighted() must return elements with probability proportional to their frequency.Step 2: Evaluate options
Maintain a list with each element repeated as many times as its frequency; getRandomWeighted() picks uniformly from this list. duplicates elements in list, causing O(n) space and slow updates. Use the existing list and hash map without changes; getRandomWeighted() is same as getRandom(). ignores weighting. Store frequencies in a balanced BST and perform weighted random selection by traversing the tree. adds complexity and O(log n) operations. Use a hash map from element to frequency and sample elements proportionally using a prefix sum array updated on insert/remove. maintains frequencies and prefix sums, enabling O(log n) or better weighted sampling with efficient updates.Step 3: Choose best tradeoff
Use a hash map from element to frequency and sample elements proportionally using a prefix sum array updated on insert/remove. is the most efficient and scalable approach to support weighted random sampling with dynamic updates.Final Answer:
Option B -> Option BQuick Check:
Prefix sums enable efficient weighted sampling [OK]
- Using naive repeated list causing high space
- Ignoring frequency updates
- Assuming getRandom() suffices for weighted sampling
