LearnFrenzy
Sign in Take a free test

DSA & Algorithms · Topic 4 of 12

Stacks and Queues: questions with answers

LIFO and FIFO operations, balanced brackets, infix to postfix, monotonic stacks, deques and sliding windows, queues from stacks and circular buffers.

Questions
45
Timed sets
3
Answers run or computed
32
Your accuracy
–

What this topic covers

15 subtopics
  • Monotonic stack7
  • Queue operations5
  • Applications4
  • Design4
  • Stack simulation4
  • Balanced brackets3
  • Deque3
  • Stack operations3
  • Basics2
  • Circular queues2
  • Expression conversion2
  • Expression evaluation2
Show all 15 subtopics
  • Stack permutations2
  • Library stacks1
  • Priority queues1
  • Beginner 15
  • Intermediate 15
  • Advanced 15

About this topic

A stack is last-in-first-out: push and pop happen at the top. A queue is first-in-first-out: enqueue at the rear, dequeue at the front. Both are O(1) per operation, and both hide inside many algorithms – the call stack in recursion, the queue in breadth-first search. The pattern interviewers love most is the monotonic stack: keep the stack sorted, and every "next greater / previous smaller" question becomes a single O(n) pass.

Free preview

Sample questions

Try each one first, then open the answer for the explanation and why the other options are wrong.

Question 1 Stack operations Beginner Practice Answer computed

Starting with an empty stack: push 1, push 2, push 3, pop, push 4, pop, push 5. What is in the stack, bottom to top?

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  1. 1
  2. 5
  1. 5
  2. 2
  3. 1
  1. 1
  2. 2
  3. 5
Show answer and explanation
Correct answer D

Explanation

Each pop removes the most recent push: 3 is popped, then 4. What remains is 1, 2 with 5 on top.

Why the other options are wrong

  • ATwo elements were popped.
  • BOnly 3 and 4 were popped.
  • CThat is top to bottom.
Question 2 Balanced brackets Beginner Practice Code executed

What does this print?

Python main.py
def balanced(s):
    pairs = {')': '(', ']': '[', '}': '{'}
    st = []
    for ch in s:
        if ch in '([{':
            st.append(ch)
        elif not st or st.pop() != pairs[ch]:
            return False
    return not st

print(balanced("{[()()]}"), balanced("([)]"), balanced("(("))
True False False
True True False
True False True
False False False
Show answer and explanation
Correct answer A

Explanation

Each closing bracket must match the most recent unmatched opener – exactly what the stack top holds. In "([)]" the ) meets [ on top. "((" leaves unmatched openers, so the final not st is False.

Why the other options are wrong

  • BIn "([)]" the brackets cross.
  • CUnclosed openers remain in "((".
  • D"{[()()]}" is properly nested.
Question 3 Expression conversion Intermediate Practice Answer computed

Convert the infix expression a + b * c - d to postfix (usual precedence, left-to-right for equal precedence).

abc*+d-
abcd-*+
+a*b-cd
ab+c*d-
Show answer and explanation
Correct answer A

Explanation

* binds tighter, so b c * is formed first and added to a; then d is subtracted from the result: ((a + (b * c)) − d). The shunting-yard algorithm pops operators of higher or equal precedence before pushing a new one.

Why the other options are wrong

  • BThat is a + (b * (c − d)).
  • CThat is prefix-like, not postfix.
  • DThat is ((a + b) * c) − d.
Question 4 Monotonic stack Advanced Practice Answer computed

The array gives bar heights of a histogram (each bar has width 1). What is the area of the largest rectangle?

Bar heights
h
  1. 02
  2. 11
  3. 25
  4. 36
  5. 42
  6. 53

The answer is a number – work it out, then open the solution.

Show answer and explanation
Correct answer 10

Explanation

Bars 5 and 6 together give height 5 × width 2 = 10. With an increasing stack, each bar learns where it stops extending on the left and right when it is popped: O(n).

41 more questions in the timed setsSame format as the samples, with a timer, a question palette and a full review at the end.
Start practising

DSA & Algorithms

All 12 topics

Topics, levels and full mocks
  1. 01 Time and Space Complexity45 questions · 3 levels
  2. 02 Arrays and Strings45 questions · 3 levels
  3. 03 Linked Lists45 questions · 3 levels
  4. 04 Stacks and Queues45 questions · 3 levels
  5. 05 Hashing45 questions · 3 levels
  6. 06 Recursion and Backtracking45 questions · 3 levels
  7. 07 Sorting and Searching45 questions · 3 levels
  8. 08 Trees and BST45 questions · 3 levels
  9. 09 Heaps and Priority Queues45 questions · 3 levels
  10. 10 Graphs45 questions · 3 levels
  11. 11 Dynamic Programming45 questions · 3 levels
  12. 12 Greedy and Bit Manipulation45 questions · 3 levels

Search LearnFrenzy

Esc

Quick links