Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
▶
Steps
setup
Initialize dummy head, tail, and size
Create a dummy head node with value 0 and no next node. Set tail pointer to dummy and size counter to 0.
💡 Dummy head simplifies edge cases by providing a fixed start; tail pointer helps efficient tail insertions; size tracks list length for quick index validation.
Size check quickly rejects invalid indices, saving time and avoiding errors.
Practice
(1/5)
1. Consider the following code snippet implementing the optimal approach to copy a list with random pointers. Given the input list: 1 -> 2 -> 3, where node 1's random points to node 3, node 2's random points to node 1, and node 3's random is null, what is the value of copy_curr.random.val after the last iteration of the separation step (Step 3)?
easy
A. 1
B. 3
C. None (random pointer is null)
D. 2
Solution
Step 1: Trace Step 1 (Interleaving nodes)
Original list: 1->2->3. After interleaving: 1->1'->2->2'->3->3'.
Step 2: Trace Step 2 (Assign random pointers)
Node 1's random points to 3, so 1'.random = 3'. Node 2's random points to 1, so 2'.random = 1'. Node 3's random is null, so 3'.random = null.
Step 3: Trace Step 3 (Separate lists)
After separation, copy_curr starts at 1'. After last iteration, copy_curr is at 3'. Its random pointer is null, so copy_curr.random.val does not exist.
Final Answer:
Option C -> Option C
Quick Check:
copy_curr.random is null after last iteration [OK]
Hint: Random pointers point to copied nodes via original's next [OK]
Common Mistakes:
Confusing original and copied nodes during separation
Off-by-one error in advancing copy_curr
Assuming random pointers remain unchanged
2. Consider the following buggy recursive code to convert a binary number in a linked list to an integer. Which line contains the subtle bug that causes incorrect output for single-node lists?
medium
A. Line X: using addition instead of bitwise OR to accumulate bits
B. Line 7: base case check for None node
C. Line 3: __init__ method of ListNode
D. Line 9: recursive call with node.next and updated acc
Solution
Step 1: Identify the accumulation operation
The code uses bitwise OR instead of addition to combine bits: acc = (acc << 1) | node.val.
Step 2: Understand impact on single-node lists
Bitwise OR correctly sets bits without carry, ensuring accurate accumulation for all list lengths.
Final Answer:
Option A -> Option A
Quick Check:
Bitwise OR correctly sets bits without carry, addition may overflow [OK]
Hint: Use bitwise OR, not addition, to accumulate bits [OK]
Common Mistakes:
Using + instead of | causes wrong bit accumulation
Misunderstanding bitwise operations vs arithmetic
Assuming addition and OR are interchangeable for bits
3. The following code attempts to implement the two stacks browser history. Identify the line containing the subtle bug that causes forward navigation to return outdated pages after visiting a new URL.
Hint: Always check curr is not None before accessing curr.next [OK]
Common Mistakes:
Forgetting None check before pointer advance
Incorrectly cutting list by setting next to None too early
Misusing dummy heads
5. Suppose the problem is modified so that nodes can be reused multiple times (i.e., after reversing a group, nodes can appear again in subsequent groups). Which of the following changes to the algorithm correctly handles this scenario?
hard
A. Modify the algorithm to create new nodes for each group reversal to avoid modifying original nodes in place.
B. This problem cannot be solved by reversal; instead, use a queue to simulate repeated node usage.
C. Use recursion with memoization to store reversed groups and reuse them without modifying the original list.
D. Use the same iterative reversal approach but reset pointers to allow reusing nodes in multiple groups.
Solution
Step 1: Understand node reuse implication
Reusing nodes means original nodes must remain unchanged or duplicated to appear multiple times.
Step 2: Evaluate algorithm modifications
In-place reversal modifies nodes destructively, so creating new nodes for each group is necessary to preserve original nodes.
Step 3: Assess other options
Resetting pointers (A) breaks list integrity; recursion with memoization (C) is complex and not standard; queue simulation (B) does not solve reversal reuse.
Final Answer:
Option A -> Option A
Quick Check:
Duplicating nodes preserves original list for reuse [OK]
Hint: Node reuse requires duplication, not in-place reversal [OK]