Practice
Solution
Step 1: Trace first iteration starting at index 0
nums[0]=2 (positive), direction is forward. slow and fast start at 0.Step 2: Move slow and fast pointers
slow moves to index (0+2)%5=2, fast moves two steps: first to 2, then to (2+1)%5=3. Both nums[2] and nums[3] are positive, direction consistent.Step 3: Next iteration
slow moves to (2+1)%5=3, fast moves two steps: from 3 to (3+2)%5=0, then from 0 to (0+2)%5=2. slow=3, fast=2, not equal yet.Step 4: Next iteration
slow moves to (3+2)%5=0, fast moves two steps: from 2 to (2+1)%5=3, then from 3 to (3+2)%5=0. slow=0, fast=0, pointers meet.Step 5: Check cycle length
Check if slow == next_index(slow): next_index(0) = 2, not equal, so cycle length > 1.Final Answer:
Option A -> Option AQuick Check:
Cycle detected with consistent direction and length > 1 [OK]
- Confusing slow and fast pointer positions
- Ignoring direction check
- Mistaking single-element loop as valid
1 -> 2 -> 3 -> 4 -> 5. What is the printed output after execution?Solution
Step 1: Trace recursion from end
Recursion returns indices from the end: node 5 returns 1, node 4 returns 2, node 3 returns 3, etc.Step 2: Identify node to remove
When idx == n+1 = 3, node 3's next pointer skips node 4, effectively removing node 4.Final Answer:
Option B -> Option BQuick Check:
Output matches list with 4 removed: 1 2 3 5 [OK]
- Removing the node at idx == n instead of n+1
- Off-by-one errors in recursion index
- Confusing which node to skip
Solution
Step 1: Identify cost per iteration
Each iteration computes sum of squares of digits. Number of digits in n is proportional to log n, so each iteration is O(log n).Step 2: Multiply by number of iterations k
The process repeats k times until reaching 1 or cycle. Total time is O(k * log n).Final Answer:
Option B -> Option BQuick Check:
Sum of digits per iteration is log n, repeated k times -> O(k * log n) [OK]
- Confusing n with number of digits, assuming O(n) per iteration
Solution
Step 1: Identify termination condition
The code must set right.next = None when left meets right or adjacent to avoid cycles.Step 2: Locate missing termination
The commented line misses 'right.next = None', causing the list to form cycles.Final Answer:
Option A -> Option AQuick Check:
Missing termination causes infinite traversal [OK]
- Forgetting to set right.next = null
- Misplacing stop flag
- Incorrect pointer updates
Solution
Step 1: Analyze counting nodes
Counting total nodes requires traversing the entire list once -> O(n).Step 2: Analyze splitting parts
Splitting involves iterating over k parts and moving pointers, total steps sum to n nodes plus k iterations -> O(n + k).Final Answer:
Option A -> Option AQuick Check:
Counting + splitting both contribute; total is O(n + k) [OK]
- Assuming O(n*k) due to nested loops
- Ignoring counting step
- Confusing k with n
