Question 2Easy
What is the postfix form of the infix expression p + q * r - s / t?
- pqr*+s-t/
- pq+r*st/-
- pqr*+st/-
- pqr*st/-+
Answer: C. pqr*+st/-
Explanation
Using operator precedence (^ highest and right-associative, then * /, then + -), the postfix form is pqr*+st/-.
Step-by-step solution
- * binds tighter than + and -: p + (q*r) - (s/t).
- + and - are left-associative: (p + q*r) - s/t.
- Postfix: p q r * + s t / -.
Why the other options are wrong
- A. Treats it as ((p + q*r) - s) / t.
- B. Treats it as (p + q) * r.
- D. Puts + last, which gives p + (q*r - s/t), a different grouping order.
Tip: Same precedence, left-assoc: pop before pushing.
Question 3Easy
What is the value of the postfix expression 8 2 3 * - 4 2 / + ? (/ is integer division)
- 4
- 3
- 8
- 5
Answer: A. 4
Explanation
Push operands; on an operator pop b then a and push a op b. The final value on the stack is 4.
Step-by-step solution
- 8 2 3 *: 2 * 3 = 6; then 8 - 6 = 2.
- 4 2 /: 4 / 2 = 2.
- +: 2 + 2 = 4.
Why the other options are wrong
- B. Not a result of any step order here.
- C. 8 is just the first operand.
- D. Not a result of correct evaluation.
Tip: Postfix: second pop is the LEFT operand.
Question 4Easy
Which of these bracket strings is balanced?
- ([)]
- (([]){})
- {[(])}
- (()
Answer: B. (([]){})
Explanation
Push each opening bracket and pop when the matching closing bracket arrives. Only (([]){}) always pops a matching opener and ends with an empty stack.
Step-by-step solution
- Push each opener; a closer must match the top.
- (([]){}): ( ( [ ] ) { } ) all match in order.
- Stack is empty at the end: balanced.
Why the other options are wrong
- A. ) arrives when [ is on top.
- C. ] arrives when ( is on top.
- D. One ( is never closed.
Tip: Balanced = every closer matches the top, and the stack ends empty.
Question 5Medium
A string is encoded as k[s] meaning s repeated k times (brackets may nest). What does "3[a2[c]]" decode to?
- acacac
- accaccacc
- aaacc
- acccacc
Answer: B. accaccacc
Explanation
Use a stack of (partial string, repeat count); on ']' pop and append the repeated inner string to the outer one. The result is accaccacc.
Step-by-step solution
- Inner: 2[c] = cc, so a2[c] = acc.
- 3[acc] = accaccacc.
- Answer: accaccacc.
Why the other options are wrong
- A. Treats 2[c] as c.
- C. Expands in the wrong order.
- D. Repeats only part of the pattern.
Tip: Decode from the innermost bracket outward.
Question 6Medium
What does this code print?
def f(s):
st = []
for ch in s:
if st and st[-1] == ch:
st.pop()
else:
st.append(ch)
return "".join(st)
print(f("abbaca"), f("azxxzy"))
- a y
- ca azy
- ca ay
- aca ay
Answer: C. ca ay
Explanation
Adjacent equal pairs are removed repeatedly: abbaca -> aaca -> ca, and azxxzy -> azzy -> ay.
Step-by-step solution
- abbaca: push a, push b, b matches so pop, a matches so pop, push c, push a: 'ca'.
- azxxzy: push a, z, x; x matches so pop; z matches so pop; push y: 'ay'.
- Output 'ca ay'.
Why the other options are wrong
- A. Removes too many characters.
- B. Forgets that removing xx makes zz adjacent.
- D. Stops after one round of removals.
Question 7Medium
What is the postfix form of the infix expression m * (n + o * p) - q?
- mnop*+q-*
- mnop+q-
- mno+p**q-
- mnop*+*q-
Answer: D. mnop*+*q-
Explanation
Using operator precedence (^ highest and right-associative, then * /, then + -), the postfix form is mnop*+*q-.
Step-by-step solution
- Inside the brackets: n + (o * p) gives n o p * +.
- m times that: m n o p * + *.
- Then subtract q: m n o p * + * q -.
Why the other options are wrong
- A. Applies - before *, giving m * (... - q).
- B. Adds before multiplying o and p.
- C. Groups (n + o) wrongly.
Tip: In infix to postfix, operand order never changes.
Question 8Medium
The span of a stock's price on day i is the number of consecutive days up to and including day i on which the price was <= the price on day i. What are the spans for prices [30, 35, 40, 38, 35, 45, 20]?
- [1, 2, 3, 1, 1, 6, 2]
- [1, 2, 3, 1, 1, 6, 1]
- [1, 2, 3, 1, 1, 3, 1]
- [1, 1, 2, 3, 1, 1, 6]
Answer: B. [1, 2, 3, 1, 1, 6, 1]
Explanation
A stack of (price, span) pairs pops every earlier price <= today's and adds its span. Spans: [1, 2, 3, 1, 1, 6, 1].
Step-by-step solution
- 30:1, 35:2, 40:3.
- 38:1, 35:1 (each stops at a higher price).
- 45 beats everything: 6; 20 is lower than 45: 1.
Why the other options are wrong
- A. 20 < 45, so its span is 1.
- C. 45 is higher than all previous prices, so span 6.
- D. Shifted values.
Question 9Hard
What is the area of the largest rectangle in a histogram with bar heights [6, 2, 5, 4, 5, 1, 6] (each bar has width 1)?
- 10
- 12
- 14
- 6
Answer: B. 12
Explanation
With a monotonic stack each bar finds its nearest smaller bar on both sides, and its widest rectangle is height * (right - left - 1). The best is 12.
Step-by-step solution
- Bars 5, 4, 5 (indices 2-4) have min 4 over width 3: 12.
- Height 2 across indices 0-4 gives 10.
- Other options are smaller: 6, 7. Max 12.
Why the other options are wrong
- A. 10 uses height 2 over 5 bars, smaller than 12.
- C. 14 has no valid width/height pair here.
- D. 6 is a single tall bar.
Question 10Hard
A min-stack keeps an auxiliary stack: on push, the value is also pushed onto it if it is <= the auxiliary top; on pop, the auxiliary top is popped if it equals the popped value. After push 5, push 3, push 7, push 3, pop, pop, push 2, pop, pop, what is getMin() and what does the auxiliary stack hold (bottom to top)?
getMin() = 3, auxiliary = [5, 3]getMin() = 5, auxiliary = [5]getMin() = 2, auxiliary = [5, 2]getMin() = 5, auxiliary = [5, 3, 3]
Answer: B. getMin() = 5, auxiliary = [5]
Explanation
The main stack ends as [5]. Pushing 3 twice put 3 twice on the auxiliary stack, and both were popped later, leaving [5].
Step-by-step solution
- push 5, 3: aux [5, 3]; push 7: no; push 3 (3 <= 3): aux [5, 3, 3].
- pop 3: aux [5, 3]; pop 7: no change; push 2: aux [5, 3, 2]; pop 2: aux [5, 3].
- pop 3: aux [5]. Min = 5.
Why the other options are wrong
- A. Misses the last pop of 3.
- C. 2 was already popped.
- D. Ignores the pops that removed 3s from aux.
Tip: Use <= when pushing to the min stack, or duplicates break it.