Practice
Solution
Step 1: Trace enQueue(1), enQueue(2), enQueue(3)
Initially, head=0, tail=0, size=0. After enQueue(1): tail=1, size=1; enQueue(2): tail=2, size=2; enQueue(3): tail=0 (wrap-around), size=3.Step 2: Trace deQueue() and enQueue(4)
deQueue() moves head from 0 to 1, size=2. enQueue(4) inserts at tail=0, then tail=1, size=3.Final Answer:
Option A -> Option AQuick Check:
Tail pointer wraps modulo capacity, final tail=1 after last enQueue [OK]
- Forgetting modulo wrap-around
- Off-by-one tail position
- Confusing head and tail pointers
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 missing mapping assignment
The code pops smaller elements but does not assign their next greater element in next_greater_map, causing missing or incorrect results.Step 2: Confirm other lines are correct
The while condition, set removal, and final assignment of -1 are all correct and necessary for correctness and efficiency.Final Answer:
Option D -> Option DQuick Check:
Without mapping assignment, queries for nums1 elements fail [OK]
- Forgetting to assign next greater element after popping from stack
- Incorrectly removing elements from nums1_set too early
- Misplacing the final assignment of -1 for leftover stack elements
Solution
Step 1: Identify loop iterations
The loop runs over doubled array of length 2n, so 2n iterations.Step 2: Analyze stack operations
Each element index is pushed once and popped once at most, so total stack operations are O(n). The doubling does not cause repeated pushes for the same index.Final Answer:
Option A -> Option AQuick Check:
Doubling array length does not multiply complexity beyond linear [OK]
- Assuming doubling array doubles complexity to O(n²)
- Confusing stack operations with sorting complexity
- Ignoring that indices are pushed only once
Solution
Step 1: Understand impact of negative prices
Negative prices do not affect the monotonic stack logic, but to handle spans correctly, storing spans with prices helps accumulate consecutive spans efficiently.Step 2: Why storing (price, span) pairs works
When popping smaller or equal prices, their spans can be added to the current span, allowing correct span calculation even with negative or mixed values.Step 3: Why other options fail
Changing while condition to strict less breaks equal price handling. Balanced BST is unnecessary overhead. No change ignores span accumulation needed for correctness.Final Answer:
Option A -> Option AQuick Check:
Storing spans with prices is a known robust solution [OK]
- Assuming no change needed
- Using strict less instead of less or equal
