Stacks & Queues Questions with Answers

66 practice questions on Stacks & Queues (Data Structures & Algorithms) with answers and step-by-step solutions. Try 10 samples below, then practise all 66 free in the AptiNest app.

Sample questions

Question 1Easy

What is the value of the prefix expression + 9 * 2 - 6 3? (/ is integer division)

  1. 15
  2. 30
  3. 9
  4. 17

Answer: A. 15

Explanation

Scan from right to left, push operands, and on an operator pop a (left operand) then b and push a op b. The result is 15.

Step-by-step solution

  1. - 6 3 = 3.
  2. * 2 3 = 6.
  3. + 9 6 = 15.

Why the other options are wrong

  • B. 30 comes from adding first, (9 + 6) * 2, which ignores the prefix order.
  • C. 9 is just the first operand; 6 still has to be added.
  • D. 17 does not follow from the expression; 9 + 2 * (6 - 3) = 15.

Question 2Easy

What is the postfix form of the infix expression p + q * r - s / t?

  1. pqr*+s-t/
  2. pq+r*st/-
  3. pqr*+st/-
  4. 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

  1. * binds tighter than + and -: p + (q*r) - (s/t).
  2. + and - are left-associative: (p + q*r) - s/t.
  3. 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)

  1. 4
  2. 3
  3. 8
  4. 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

  1. 8 2 3 *: 2 * 3 = 6; then 8 - 6 = 2.
  2. 4 2 /: 4 / 2 = 2.
  3. +: 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?

  1. ([)]
  2. (([]){})
  3. {[(])}
  4. (()

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

  1. Push each opener; a closer must match the top.
  2. (([]){}): ( ( [ ] ) { } ) all match in order.
  3. 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?

  1. acacac
  2. accaccacc
  3. aaacc
  4. 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

  1. Inner: 2[c] = cc, so a2[c] = acc.
  2. 3[acc] = accaccacc.
  3. 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"))
  1. a y
  2. ca azy
  3. ca ay
  4. aca ay

Answer: C. ca ay

Explanation

Adjacent equal pairs are removed repeatedly: abbaca -> aaca -> ca, and azxxzy -> azzy -> ay.

Step-by-step solution

  1. abbaca: push a, push b, b matches so pop, a matches so pop, push c, push a: 'ca'.
  2. azxxzy: push a, z, x; x matches so pop; z matches so pop; push y: 'ay'.
  3. 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?

  1. mnop*+q-*
  2. mnop+q-
  3. mno+p**q-
  4. 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

  1. Inside the brackets: n + (o * p) gives n o p * +.
  2. m times that: m n o p * + *.
  3. 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. [1, 2, 3, 1, 1, 6, 2]
  2. [1, 2, 3, 1, 1, 6, 1]
  3. [1, 2, 3, 1, 1, 3, 1]
  4. [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

  1. 30:1, 35:2, 40:3.
  2. 38:1, 35:1 (each stops at a higher price).
  3. 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)?

  1. 10
  2. 12
  3. 14
  4. 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

  1. Bars 5, 4, 5 (indices 2-4) have min 4 over width 3: 12.
  2. Height 2 across indices 0-4 gives 10.
  3. 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)?

  1. getMin() = 3, auxiliary = [5, 3]
  2. getMin() = 5, auxiliary = [5]
  3. getMin() = 2, auxiliary = [5, 2]
  4. 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

  1. push 5, 3: aux [5, 3]; push 7: no; push 3 (3 <= 3): aux [5, 3, 3].
  2. pop 3: aux [5, 3]; pop 7: no change; push 2: aux [5, 3, 2]; pop 2: aux [5, 3].
  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.

Practise all 66 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Data Structures & Algorithms topics · All topics