Practice
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:
stack.pop()
alive = False
else:
alive = False
if alive:
stack.append(a)
return stack
print(asteroidCollision([8, -8, 5, -10]))
Solution
Step 1: Trace collision between 8 and -8
8 and -8 are equal size but opposite direction, both explode -> stack empty.Step 2: Process 5 and then -10
5 is pushed; -10 collides with 5, since 10 > 5, 5 pops, -10 remains and is pushed.Final Answer:
Option B -> Option BQuick Check:
Final stack contains only [-10] after resolving collisions [OK]
- Forgetting to pop both equal asteroids
- Appending asteroid after it exploded
- Not handling multiple collisions in while loop
nums = [2, 1, 3] and k = 2?Solution
Step 1: Trace iteration i=0
dq empty, append index 0 (value 2). No output yet since i < k-1.Step 2: Trace iteration i=1
nums[1]=1 < nums[0]=2, so append index 1. Window size reached, max is nums[dq[0]]=2.Step 3: Trace iteration i=2
nums[2]=3 > nums[1]=1 pop index 1, nums[2]=3 > nums[0]=2 pop index 0, append index 2. Max is nums[2]=3.Final Answer:
Option D -> Option DQuick Check:
Output matches expected sliding window max [OK]
- Forgetting to remove out-of-window indices
- Appending max too early or late
from collections import deque
def first_unique_stream(stream):
freq = [0] * 26
q = deque()
result = []
for ch in stream:
idx = ord(ch) - ord('a')
freq[idx] += 1
if freq[idx] == 1:
q.append(ch)
# BUG: Missing removal of duplicates from queue front
result.append(q[0] if q else '#')
return result
Solution
Step 1: Identify missing logic
The code does not remove characters from the queue front when their frequency exceeds 1.Step 2: Understand impact
Without popping duplicates, the queue front may be a repeated character, causing wrong first unique output.Final Answer:
Option D -> Option DQuick Check:
Missing while loop to pop duplicates is the bug [OK]
- Forgetting to pop duplicates from queue front
- Appending duplicates multiple times
- Not handling empty queue case
Solution
Step 1: Analyze push and pop operations
Each digit is pushed exactly once onto the stack. Each digit can be popped at most once when a smaller digit arrives.Step 2: Conclude overall complexity
Since each digit is pushed and popped at most once, total operations are O(n), linear in input size.Final Answer:
Option B -> Option BQuick Check:
Monotonic stack ensures linear time by amortized analysis [OK]
- Assuming worst case k pops per digit -> O(n*k)
- Thinking sorting or nested loops are involved
Solution
Step 1: Understand operator precedence
Multiplication and division have higher precedence than addition and subtraction, so evaluation order matters.Step 2: Modify stack approach
Using two stacks (numbers and operators) allows applying higher precedence operators immediately, preserving correct order.Step 3: Why other options fail
Single stack ignoring precedence leads to wrong results; postfix conversion is valid but more complex; relying on eval() is unsafe and not interview-appropriate.Final Answer:
Option A -> Option AQuick Check:
Two-stack approach is standard for precedence and parentheses [OK]
- Ignoring precedence or relying on single stack only
