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
right after computing the right boundaries (just before calculating max_area)?Solution
Step 1: Trace right boundary computation for i=2 (height=5)
Stack is empty, so right[2] = n = 3, stack = [2]Step 2: Trace i=1 (height=1)
Pop indices with height >= 1: pop 2 (height=5), stack empty, right[1] = 3, stack = [1]Step 3: Trace i=0 (height=2)
Stack top is 1 (height=1) < 2, so right[0] = 1, stack = [1,0]Final Answer:
Option B -> Option BQuick Check:
Right boundaries correctly reflect next smaller bar indices [OK]
- Confusing indices or off-by-one in right boundary
- Not clearing stack before right boundary pass
Solution
Step 1: Trace push(3)
Stack empty, push 0, minVal=3.Step 2: Trace push(5)
diff=5-3=2>0, push 2, minVal=3.Step 3: Trace push(2)
diff=2-3=-1<0, push -1, minVal=2.Step 4: Trace pop()
pop -1 (negative), minVal=2 - (-1)=3.Step 5: getMin()
minVal=3.Final Answer:
Option B -> Option BQuick Check:
After popping 2, min is restored to 3 [OK]
- Forgetting to update minVal on pop
- Returning old minVal after pop
- Confusing top value with minVal
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
m = length of nums1 and n = length of nums2?Solution
Step 1: Analyze stack operations on nums2
Each element in nums2 is pushed once and popped at most once, so stack operations are O(n).Step 2: Analyze mapping and lookup for nums1
Mapping next greater elements for nums1 uses a hashmap and set lookups, each O(1), done m times, total O(m).Final Answer:
Option C -> Option CQuick Check:
Combined complexity is O(n + m), not O(m*n) or O(n log n) [OK]
- Assuming nested loops cause O(m*n) complexity
- Confusing sorting-based approaches with stack-based ones
- Ignoring the cost of hashmap lookups
