Practice
Solution
Step 1: Understand problem constraints
The stream consists of lowercase letters only, so a fixed-size frequency array of length 26 suffices.Step 2: Identify optimal approach
Using a frequency array plus a queue to track candidates for first unique character allows O(1) updates per character and O(n) total time.Final Answer:
Option C -> Option CQuick Check:
Frequency array + queue approach is standard for this problem [OK]
- Thinking brute force scanning is efficient enough
- Assuming DP applies here
- Using greedy without frequency tracking
nextGreaterElement([4,1,2], [1,3,4,2])?Solution
Step 1: Trace stack and map updates for nums2
Process nums2: push 1, then 3 > 1 so pop 1 -> map[1]=3; push 3; 4 > 3 pop 3 -> map[3]=4; push 4; 2 < 4 push 2. Stack ends with [4,2].Step 2: Assign -1 for remaining stack elements and build result
Pop 2 -> map[2]=-1; pop 4 -> map[4]=-1. Result for nums1=[4,1,2] is [map[4], map[1], map[2]] = [-1,3,-1].Final Answer:
Option B -> Option BQuick Check:
Matches expected output from code comments and manual trace [OK]
- Confusing order of popping leading to wrong map values
- Forgetting to assign -1 for elements with no next greater
- Mixing up indices when building the final result
def asteroidCollision(asteroids):
stack = []
for a in asteroids:
alive = True
while alive and stack and stack[-1] > 0 and a < 0:
if stack[-1] < -a:
stack.pop()
elif stack[-1] == -a:
alive = False
else:
alive = False
if alive:
stack.append(a)
return stack
Solution
Step 1: Identify behavior on equal size asteroids
When sizes are equal, both asteroids should explode (pop from stack and not append current).Step 2: Check code for equal size case
In the buggy code, stack.pop() is missing when sizes are equal, so the top asteroid remains incorrectly.Final Answer:
Option C -> Option CQuick Check:
Missing stack.pop() causes incorrect survival of equal asteroid [OK]
- Forgetting to pop stack on equal size
- Appending asteroid after it exploded
- Incorrect while loop condition
Solution
Step 1: Identify the operations per element
Each element is pushed once and popped at most once from the stack, so total stack operations are O(n).Step 2: Understand why O(n^2) is a common misconception
Nested loops in brute force suggest O(n^2), but monotonic stack avoids repeated comparisons by popping larger elements immediately.Final Answer:
Option A -> Option AQuick Check:
Stack operations linear in n, no nested iteration causing quadratic time [OK]
- Confusing brute force nested loops with stack approach
- Assuming stack operations cost log n each
- Forgetting stack pops amortize total cost
Solution
Step 1: Understand reuse changes problem nature
Reusing bars means rectangles can extend beyond original boundaries, invalidating monotonic stack assumptions.Step 2: Identify suitable approach
Dynamic programming can track cumulative widths and heights considering reuse, unlike monotonic stack which assumes unique bars.Final Answer:
Option A -> Option AQuick Check:
Monotonic stack does not handle repeated bar reuse correctly [OK]
- Assuming monotonic stack handles reuse without changes
- Trying to fix stack by storing counts only
