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
Solution
Step 1: Identify time complexity
The algorithm scans the input string once, performing constant-time operations per character, so time is O(n).Step 2: Identify space complexity
Only a fixed number of variables are used (total, last_num, current_num, current_op), so space is O(1).Final Answer:
Option A -> Option AQuick Check:
Single pass and constant variables confirm O(n) time and O(1) space [OK]
- Assuming stack usage means O(n) space
- Confusing recursion stack with iteration
- Thinking sorting operators is needed
Solution
Step 1: Analyze stack operations
Each bar is pushed once and popped at most once, so total stack operations are O(n).Step 2: Confirm no nested loops over all elements
Loops run linearly, no repeated scanning of all elements inside inner loops.Final Answer:
Option A -> Option AQuick Check:
Linear time complexity due to amortized stack operations [OK]
- Assuming stack operations cause O(n log n) or O(n^2)
- Confusing nested loops in brute force with stack approach
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
