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
n positions from the end in a singly linked list. Which approach guarantees a single-pass solution with O(n) time complexity and O(1) extra space?Solution
Step 1: Understand the problem constraints
The goal is to find thenth node from the end in a single pass with minimal extra space.Step 2: Evaluate approaches
Using two pointers with the fast pointernsteps ahead allows the slow pointer to land exactly on the target node when fast reaches the end, achieving O(n) time and O(1) space.Final Answer:
Option B -> Option BQuick Check:
Two-pointer technique is classic for single-pass linked list problems [OK]
- Confusing stack approach as single pass
- Using two passes instead of one
- Assuming recursion is O(1) space
Solution
Step 1: Analyze outer and inner loops
Each index is processed once in the outer loop; inner while loop visits elements until cycle or zero marking.Step 2: Check effect of in-place marking
Marking visited elements as zero prevents revisiting, ensuring total visits across all iterations is O(n).Final Answer:
Option A -> Option AQuick Check:
In-place marking guarantees linear time complexity [OK]
- Assuming repeated visits cause O(n²)
- Ignoring marking effect
- Confusing modulo cost as log factor
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: Understand reuse implications
If nodes are reused or list is cyclic, modifying it breaks future traversals.Step 2: Restore list after palindrome check
Reversing second half in-place must be undone to preserve original list structure.Final Answer:
Option D -> Option DQuick Check:
Restoring reversed half ensures list integrity for reuse [OK]
- Ignoring list restoration causing side effects
- Switching to stack approach unnecessarily increasing space
- Assuming array conversion is always better
