Returning -1 signals no solution; 0 misleads caller [OK]
Hint: Return -1 on failure, not 0 [OK]
Common Mistakes:
Returning 0 instead of -1 on failure
Overcounting rotations when both sides equal candidate
2. What is the time complexity of the optimal max heap approach to reorganize a string of length n with k unique characters?
medium
A. O(n²) because each character insertion may require scanning the entire string
B. O(n log k) because each of the n characters is pushed and popped from a heap of size k
C. O(k log n) because the heap operations depend on the string length
D. O(n) because each character is processed once without extra overhead
Solution
Step 1: Identify heap operations per character
Each character is pushed and popped at most once per occurrence, total n operations.
Step 2: Analyze heap size and operation cost
Heap size is at most k (unique chars), each push/pop is O(log k), so total O(n log k).
Final Answer:
Option B -> Option B
Quick Check:
Heap operations dominate, not scanning entire string [OK]
Hint: Heap ops cost O(log k) per character [OK]
Common Mistakes:
Confusing n and k in complexity
Assuming linear time without heap cost
Mistaking quadratic due to nested loops
3. What is the time complexity of the optimal Task Scheduler algorithm using a max-heap for t total tasks and m unique tasks?
medium
A. O(t log m) because each task is pushed and popped from a heap of size up to m
B. O(t + m) because counting frequencies and scheduling are linear
C. O(m log t) because heap operations depend on total tasks
D. O(t * m) because each task may be compared with all unique tasks
Solution
Step 1: Analyze heap operations
Heap size is at most m (unique tasks). Each task is pushed and popped at most once per execution.
Step 2: Calculate total operations
For t tasks, each heap operation costs O(log m), so total is O(t log m).
Final Answer:
Option A -> Option A
Quick Check:
Heap size depends on unique tasks, not total tasks [OK]
Hint: Heap operations scale with unique tasks, not total tasks [OK]
Common Mistakes:
Confusing total tasks and unique tasks
Assuming linear heap operations
Ignoring log factor in heap push/pop
4. What is the time complexity of the optimal greedy algorithm for the wiggle subsequence problem, and why might some candidates mistakenly think it is higher?
medium
A. O(n) because it scans the list once, updating counters based on difference signs
B. O(2^n) because it explores all subsequences recursively
C. O(n log n) due to sorting or binary search steps involved
D. O(n^2) because it compares each element with all previous elements
Solution
Step 1: Analyze algorithm operations
The greedy algorithm iterates through the list once, computing differences and updating counters in O(1) time per element.
Step 2: Address common misconceptions
Some candidates confuse it with brute force or DP approaches, thinking it compares pairs or explores subsequences exponentially, leading to O(n^2) or O(2^n) assumptions.
Final Answer:
Option A -> Option A
Quick Check:
Single pass with constant work per element -> O(n) [OK]
Hint: Single pass with constant updates -> O(n)
Common Mistakes:
Confusing with brute force exponential time
Assuming nested loops for comparisons
Thinking sorting is involved
5. Suppose the Jump Game problem is modified so that you can jump backward as well as forward (i.e., jumps can be negative or positive). Which of the following approaches correctly determines if you can reach the last index from the first index under this new constraint?
hard
A. Use the original greedy approach tracking max reachable index, ignoring backward jumps
B. Use a breadth-first search (BFS) or graph traversal to explore all reachable indices including backward jumps
C. Use dynamic programming with memoization to recursively check reachability from each index
D. Sort the array and apply binary search to find reachable indices efficiently
Solution
Step 1: Understand the impact of backward jumps
Backward jumps mean the problem is no longer monotonic; maxReach tracking fails as reachable indices can decrease.
Step 2: Identify suitable approach
BFS or graph traversal explores all reachable indices in any direction, correctly handling negative jumps.
Step 3: Explain why other options fail
Greedy fails due to backward jumps; DP recursion is possible but less efficient; sorting is irrelevant.
Final Answer:
Option B -> Option B
Quick Check:
BFS explores all reachable nodes regardless of jump direction [OK]
Hint: Backward jumps break greedy; BFS needed to explore all reachable indices [OK]