Practice
from collections import deque
class MyStack:
def __init__(self):
self.q = deque()
def push(self, x: int) -> None:
self.q.append(x)
for _ in range(len(self.q) - 1):
self.q.append(self.q.popleft())
def pop(self) -> int:
return self.q.popleft()
def top(self) -> int:
return self.q[0]
def empty(self) -> bool:
return not self.q
stack = MyStack()
stack.push(10)
stack.push(20)
stack.push(30)
print(stack.pop())
print(stack.top())
What is the output of the two print statements?Solution
Step 1: Trace push operations
Push 10: queue=[10]
Push 20: queue=[20,10] after rotation
Push 30: queue=[30,20,10] after rotationStep 2: Trace pop and top
Pop removes front: 30
Top returns front: 20Final Answer:
Option C -> Option CQuick Check:
Rotation puts last pushed element front -> pop returns 30, top returns 20 [OK]
- Not rotating queue correctly, so pop returns oldest element
- Confusing pop and top outputs
- Off-by-one in rotation loop
Solution
Step 1: Analyze push condition
The condition 'if diff <= 0:' updates minVal even when diff == 0, i.e., when new element equals current min.Step 2: Understand impact
Updating minVal on equal values causes minVal to be overwritten incorrectly, breaking min tracking when duplicates are popped.Final Answer:
Option C -> Option CQuick Check:
Correct condition is 'diff < 0' to update minVal only on strictly smaller values [OK]
- Using <= instead of < causes minVal errors with duplicates
- Incorrect pop minVal update
- Appending wrong values to stack
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
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 tail pointer semantics
Tail points to the next insertion index, not the last inserted element.Step 2: Calculate rear element index
The last inserted element is at (tail - 1 + capacity) modulo capacity to handle wrap-around.Final Answer:
Option B -> Option BQuick Check:
Rear element index is tail - 1 modulo capacity [OK]
- Using tail index directly
- Confusing head with rear
- Using size as index
