Practice
1. You are given a circular array where each element represents a jump length and direction (positive for forward, negative for backward). The task is to determine if there exists a cycle in the array such that the cycle is longer than one element and all jumps are in the same direction. Which algorithmic approach guarantees an optimal solution for this problem?
easy
Solution
Step 1: Understand problem constraints
The problem requires detecting cycles in a circular array with direction consistency and cycle length > 1.Step 2: Identify suitable algorithm
Fast and slow pointer cycle detection efficiently finds cycles in O(n) time while checking direction and cycle length constraints.Final Answer:
Option D -> Option DQuick Check:
Fast and slow pointers detect cycles without exhaustive search [OK]
Hint: Cycle detection with direction check -> fast-slow pointers [OK]
Common Mistakes:
- Assuming greedy jumps always find cycles
- Using DP which is inefficient here
- Brute force is correct but not optimal
2. You are given a singly linked list and need to determine the length of a cycle if one exists. Which approach guarantees an optimal solution with O(n) time and O(1) space complexity?
easy
Solution
Step 1: Understand the problem constraints
The problem requires detecting a cycle and counting its length efficiently in a linked list.Step 2: Evaluate approaches for time and space complexity
Using two pointers (fast and slow) allows cycle detection in O(n) time and O(1) space, then counting the cycle length by traversing the cycle once is optimal.Final Answer:
Option C -> Option CQuick Check:
Fast-slow pointer approach is classic for cycle detection with constant space [OK]
Hint: Fast-slow pointers detect cycles in O(1) space [OK]
Common Mistakes:
- Believing hash sets are optimal due to extra space
- Confusing DP with cycle detection
- Using recursion unnecessarily
3. Consider the following buggy code snippet for detecting a circular array loop. Which line contains the subtle bug that causes incorrect detection of single-element loops as valid cycles?
medium
Solution
Step 1: Identify where single-element loops are checked
The original code breaks if slow == next_index(slow) to avoid single-element loops.Step 2: Locate missing check
The buggy code returns True immediately when slow == fast without verifying cycle length.Final Answer:
Option B -> Option BQuick Check:
Missing single-element loop check causes false positives [OK]
Hint: Check cycle length before returning True to avoid single-element loops [OK]
Common Mistakes:
- Returning True immediately on pointer meet
- Ignoring direction consistency
- Incorrectly zeroing elements
4. What is the time complexity of the optimal Happy Number detection algorithm that uses a known cycle set and repeatedly computes the sum of squares of digits until it reaches 1 or a cycle number?
Assume n is the input number and k is the number of iterations until termination.
medium
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]
Hint: Sum of digits cost is O(log n), not O(n) [OK]
Common Mistakes:
- Confusing n with number of digits, assuming O(n) per iteration
5. The following code attempts to remove the nth node from the end of a singly linked list. Identify the line containing the subtle bug that causes incorrect behavior when removing the head node.
medium
Solution
Step 1: Understand dummy node role
Dummy node is needed to handle removal of the head node safely.Step 2: Identify missing dummy usage
Calling recurse on head directly skips dummy, so removing head node breaks list or returns wrong head.Final Answer:
Option A -> Option AQuick Check:
Missing dummy node causes incorrect removal of head [OK]
Hint: Always use dummy node to handle head removal edge case [OK]
Common Mistakes:
- Not using dummy node causing null pointer or wrong head
- Incorrectly unlinking nodes causing list corruption
- Off-by-one errors in recursion index
