Practice
1. Given the following code for a queue implemented with two stacks, what is the output of the last print statement?
q = MyQueue()
q.push(10)
q.push(20)
print(q.pop())
q.push(30)
print(q.peek())
easy
Solution
Step 1: Trace pop() after pushing 10 and 20
pop_stack is empty, transfer push_stack [10,20] reversed to pop_stack [20,10], pop() returns 10.Step 2: After pushing 30, peek() checks pop_stack which has [20], so peek returns 20
After pop(), pop_stack has [20], push_stack has [30]. peek() does not transfer because pop_stack is not empty, so peek returns 20.Final Answer:
Option B -> Option BQuick Check:
pop() returns 10, peek() returns 20 [OK]
Hint: pop transfers once, peek uses existing pop_stack [OK]
Common Mistakes:
- Assuming peek returns 10 after pop
- Forgetting transfer only when pop_stack empty
2. Consider the following Python function that computes the largest rectangle area in a histogram using a monotonic stack approach. Given the input heights = [2, 1, 5], what is the value of the variable
right after computing the right boundaries (just before calculating max_area)?easy
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]
Hint: Right boundaries found by scanning from right with stack [OK]
Common Mistakes:
- Confusing indices or off-by-one in right boundary
- Not clearing stack before right boundary pass
3. Given the following Python code for checking valid parentheses, what is the return value when the input string is "([)]"?
def isValid(s: str) -> bool:
stack = []
pairs = {')': '(', ']': '[', '}': '{'}
for char in s:
if char in pairs.values():
stack.append(char)
elif char in pairs:
if not stack or stack[-1] != pairs[char]:
return False
stack.pop()
else:
return False
return not stack
easy
Solution
Step 1: Trace input "([)]" through the code
Push '(' then push '['. Next char is ')', expected to match '[' but pairs[')'] = '(', top of stack is '[' -> mismatch -> return False immediately.Step 2: Confirm final output
Function returns False as soon as mismatch is detected.Final Answer:
Option A -> Option AQuick Check:
Mismatch detected at third character -> returns False [OK]
Hint: Mismatch causes immediate False return [OK]
Common Mistakes:
- Assuming order doesn't matter and returning True incorrectly
4. What is the time complexity of the getMin() operation in the single stack with encoded minimum values approach for the Minimum Stack problem, and why?
medium
Solution
Step 1: Understand getMin() operation
In the encoded stack approach, minVal is updated on every push/pop, so getMin() just returns minVal.Step 2: Analyze complexity
Since getMin() returns a stored variable without iteration, it runs in O(1) time.Final Answer:
Option A -> Option AQuick Check:
getMin() is a direct variable access, no loops [OK]
Hint: getMin() returns stored minVal updated on push/pop [OK]
Common Mistakes:
- Assuming getMin scans stack
- Confusing encoded values require decoding
- Thinking balanced tree needed for min tracking
5. Suppose the sliding window maximum problem is modified so that the window size
k can vary dynamically during the iteration. Which of the following approaches correctly adapts the monotonic deque solution to handle this efficiently?hard
Solution
Step 1: Understand dynamic window size impact
Window boundaries change, so indices outside the current window must be removed accordingly.Step 2: Adapt monotonic deque maintenance
Instead of rebuilding, update boundary checks and pop indices outside the new window size dynamically.Step 3: Evaluate alternatives
Rebuilding deque or switching to heap is inefficient; brute force is too slow.Final Answer:
Option A -> Option AQuick Check:
Dynamic boundary checks maintain O(n) amortized complexity [OK]
Hint: Update boundary removals dynamically, avoid rebuilding [OK]
Common Mistakes:
- Rebuilding deque wastes time
- Assuming heap is better for variable k
