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 asked to reorder it so that the nodes are arranged in the order: first node, last node, second node, second last node, and so on. Which approach guarantees an optimal in-place solution with O(n) time and O(1) extra space?
easy
Solution
Step 1: Identify the problem constraints
The problem requires reordering the list in-place with O(n) time and O(1) space.Step 2: Evaluate approaches
Brute force uses extra space, recursion uses O(n) stack space, and DP is not applicable here. The fast-slow pointer approach finds the middle, reverses the second half, and merges in-place efficiently.Final Answer:
Option D -> Option DQuick Check:
Fast-slow pointer approach is classic for in-place reorder [OK]
Hint: Fast-slow pointer + reverse + merge is classic in-place reorder [OK]
Common Mistakes:
- Thinking recursion is O(1) space
- Using DP for linked list reorder
- Assuming array storage is in-place
3. What is the time and space complexity of the optimal single-pass two-pointer approach to find the middle node of a singly linked list with n nodes?
medium
Solution
Step 1: Identify time complexity
Fast pointer moves two steps per iteration, slow moves one; total iterations proportional to n -> O(n) time.Step 2: Identify space complexity
Only two pointers used, no extra data structures -> O(1) space.Final Answer:
Option A -> Option AQuick Check:
Linear time and constant space for two-pointer traversal [OK]
Hint: Two pointers traverse list once, no extra storage [OK]
Common Mistakes:
- Confusing space with O(n) due to recursion
- Assuming nested loops cause O(n^2)
- Thinking fast pointer halves complexity to O(log n)
4. 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
5. Suppose the array can contain multiple duplicates and some numbers appear more than twice. Which modification to Floyd's cycle detection algorithm correctly finds any duplicate number?
hard
Solution
Step 1: Understand Floyd's algorithm behavior with multiple duplicates
The cycle in the array corresponds to the repeated number's indices. Even if duplicates appear multiple times, the cycle exists and Floyd's algorithm detects its entrance.Step 2: Confirm no need for multiple runs or extra data structures
Floyd's algorithm finds one duplicate per run. It does not require modification to detect duplicates repeated more than twice.Final Answer:
Option D -> Option DQuick Check:
Cycle detection finds the cycle entrance regardless of duplicate frequency [OK]
Hint: Cycle entrance corresponds to duplicate regardless of count [OK]
Common Mistakes:
- Assuming Floyd's algorithm only works if duplicate appears twice
- Thinking multiple runs or extra space are needed
- Confusing cycle detection with hash-based methods
