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
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
def isHappy(n: int) -> bool:
def get_next(number):
total_sum = 0
while number > 0:
digit = number % 10
total_sum += digit * digit
number //= 10
return total_sum
def helper(num):
if num == 1:
return true
return helper(get_next(num))
return helper(n)
Solution
Step 1: Analyze recursion base cases
The code only stops recursion if num == 1. It does not detect cycles, so for unhappy numbers it recurses infinitely.Step 2: Identify missing cycle detection
Without tracking visited numbers or using fast-slow pointers, the recursion never terminates for cycles, causing stack overflow.Final Answer:
Option A -> Option AQuick Check:
Infinite recursion due to missing cycle detection base case [OK]
- Assuming recursion stops at cycles without explicit detection
Solution
Step 1: Check loop condition for pointer safety
The loop condition only checks if fast is not null, but fast.next may be null causing runtime error on fast.next.next.Step 2: Confirm other lines are correct
Slow moves one step, fast moves two steps correctly; length counting starts at 1 correctly.Final Answer:
Option D -> Option DQuick Check:
Missing fast.next check causes null pointer dereference [OK]
- Forgetting fast.next check
- Off-by-one in length counting
- Swapping slow and fast pointer steps
Solution
Step 1: Understand midpoint selection
For odd-length lists, slow points to the middle node, which should be skipped before reversal.Step 2: Identify bug in reversal start
Reversing from slow includes the middle node, causing mismatch in comparison.Final Answer:
Option A -> Option AQuick Check:
Correct approach skips middle node before reversal on odd-length lists [OK]
- Reversing from slow without skipping middle node
- Incorrect fast/slow pointer advancement
- Not handling odd-length lists separately
