Stack and Queue Applications: Worked Traces for NSCT
Stack and queue application questions hand you an expression, a bracket string or a sequence of queue operations and ask for the result of running the algorithm by hand: the postfix form, the value, the point where a checker fails, or the final front and rear indices. Each one is a short mechanical trace, so marks come from careful bookkeeping rather than theory. Data Structures & Algorithms carries 10% of the NSCT according to HEC's Areas of Competencies, and 2026 candidates averaged only 40.2% accuracy in it, per the HEC NSCT report and APP.
This page deepens the stack and queue sections of Linear Data Structures MCQs, which define the structures and their operations. Here every algorithm is traced step by step, and each trace was checked by running it in Python.
The method, step by step
- Name the structure. Most-recent-first problems (brackets, expressions, undo) use a stack; first-come-first-served problems (scheduling, buffering, BFS) use a queue.
- Draw a three-column table: input symbol, stack or queue state, output.
- Write the precedence ladder before converting: ^ (3, right-associative), * and / (2), + and - (1, both left-associative).
- Process one symbol per row. Never do two pops in your head.
- Finish cleanly: pop everything left at the end for conversions; check the stack is empty for brackets; recompute front and rear for queues.
Infix to postfix
The shunting-yard method converts an infix expression (operators between operands) to postfix (operators after operands). Rules:
- Operand: write it to the output.
- "(": push it.
- ")": pop to the output until "(" and discard both brackets.
- Operator: while the top is an operator with higher precedence, or equal precedence and the incoming operator is left-associative, pop it to the output. Then push the incoming operator.
- End: pop everything left.
Worked example. Convert K + L * (M - N) ^ P ^ Q / R.
| Read | Stack (bottom to top) | Output |
|---|---|---|
| K | K | |
| + | + | K |
| L | + | K L |
| * | + * | K L |
| ( | + * ( | K L |
| M | + * ( | K L M |
| - | + * ( - | K L M |
| N | + * ( - | K L M N |
| ) | + * | K L M N - |
| ^ | + * ^ | K L M N - |
| P | + * ^ | K L M N - P |
| ^ | + * ^ ^ | K L M N - P |
| Q | + * ^ ^ | K L M N - P Q |
| / | + / | K L M N - P Q ^ ^ * |
| R | + / | K L M N - P Q ^ ^ * R |
| end | K L M N - P Q ^ ^ * R / + |
Answer: K L M N - P Q ^ ^ * R / +. Two moments decide it. The second ^ does not pop the first, because ^ is right-associative. When / arrives it pops both ^ and the * (equal precedence, left-associative), but stops at + (lower).
Infix to prefix
Prefix (Polish) notation puts each operator before its operands. The safest exam method is to bracket the expression fully, then move each operator to the front of its bracket.
Worked example. (A - B) / C * (D + E). Since / and * are left-associative, this is ((A - B) / C) * (D + E).
- Inner pieces: A - B becomes - A B, and D + E becomes + D E.
- Division: / (- A B) C.
- Multiplication: * (/ - A B C) (+ D E).
Answer: * / - A B C + D E. For comparison, the postfix form is A B - C / D E + . Note that prefix is not* simply the postfix reversed.
Evaluating postfix and prefix
For postfix, scan left to right: push numbers; on an operator, pop the right operand first, then the left, and push the result.
Worked example. Evaluate 7 2 3 * - 4 2 ^ +.
| Read | Stack after |
|---|---|
| 7, 2, 3 | 7, 2, 3 |
| * | 7, 6 |
| - | 1 |
| 4, 2 | 1, 4, 2 |
| ^ | 1, 16 |
| + | 17 |
Answer: 17, which is 7 - 2 * 3 + 4 ^ 2.
For prefix, scan right to left and pop the left operand first. Worked example: - * + 4 2 3 / 8 2 means (4 + 2) * 3 - 8 / 2 = 18 - 4 = 14.
Balanced brackets
The checker pushes every opening bracket. On a closing bracket it fails if the stack is empty or the top is the wrong type; otherwise it pops. At the end the string is balanced only if the stack is empty.
Worked example. Three strings:
| String | What happens | Result |
|---|---|---|
| {[()()]} | Pushes reach depth 3; every closer matches its top | Balanced, stack empty |
| ())( | Third symbol ")" finds an empty stack | Fails at index 2 (0-based) |
| [{()}]( | Everything matches until the last "(" stays on the stack | Fails at the end, "(" left open |
Exam angle: questions ask for the first failure point or the maximum stack depth. Depth is the deepest nesting, here 3 for {[()()]}.
Circular queues
A circular queue reuses array slots by wrapping indices with the modulus. In the common textbook version, front = rear = 0 means empty; enqueue does rear = (rear + 1) mod n then stores; dequeue does front = (front + 1) mod n then returns that slot. The queue is full when (rear + 1) mod n = front, so one slot always stays empty. The number of elements is (rear - front + n) mod n.
Worked example. n = 5. Operations: enqueue A, B, C; dequeue; enqueue D, E; dequeue; enqueue F, G.
| Operation | front | rear | Note |
|---|---|---|---|
| E A, E B, E C | 0 | 3 | slots 1 to 3 hold A B C |
| D | 1 | 3 | A out |
| E D | 1 | 4 | |
| E E | 1 | 0 | rear wraps to 0 |
| D | 2 | 0 | B out |
| E F | 2 | 1 | count = (1 - 2 + 5) mod 5 = 4, now full |
| E G | 2 | 1 | overflow, because (1 + 1) mod 5 = front |
The queue now holds C, D, E, F, with C next out. A version that keeps a separate count variable could have stored G as well.
Speed tricks
- Full-bracket check for conversions: bracket the infix by precedence first; then postfix and prefix are just "move each operator to the end or front of its bracket".
- Operand order never changes between infix, prefix and postfix. Eliminate any option where the letters are out of order.
- Postfix sanity count: every postfix expression has one fewer operator than operands, and at every prefix of the scan operands must outnumber operators.
- Queue count formula (rear - front + n) mod n is faster than replaying every operation when only the size is asked.
Common mistakes
- Popping on equal precedence for ^. That turns A ^ B ^ C into A B ^ C ^, which means (A ^ B) ^ C.
- Swapping operands in subtraction or division during evaluation. In postfix, the first value popped is the right operand.
- Declaring a bracket string balanced because the counts match. "{(})" has matching counts but fails at its third symbol.
- Forgetting the wrap: rear = 4 in a size-5 array moves to 0, not 5.
- Treating prefix as reversed postfix. It holds only for some expressions.
Practice set
These are separate from the quiz below.
- Convert A + B - C * D to postfix.
- Convert (A + B) * (C - D) ^ E to postfix.
- Convert A / B ^ C - D to prefix.
- Evaluate the postfix 6 3 / 4 2 - *.
- Evaluate the postfix 9 2 2 ^ ^.
- Evaluate the prefix * - 8 3 + 1 2.
- Is ((a+b)*[c-d]) balanced, and what is its maximum stack depth?
- Where does the checker first fail on (())) ?
- A circular queue of size 7 (one slot kept empty) has front = 5 and rear = 2. How many elements does it hold?
Show answers
- A B + C D * -. The + is popped when - arrives (equal precedence, left-associative).
- A B + C D - E ^ . ^ binds tighter than , so it applies to (C - D) and E first.
-
- / A ^ B C D. The expression is (A / (B ^ C)) - D.
-
- 6 / 3 = 2, 4 - 2 = 2, and 2 * 2 = 4.
-
- 2 ^ 2 = 4, then 9 ^ 4 = 6561; the right-associative grouping is built in.
-
- (8 - 3) * (1 + 2) = 5 * 3.
- Yes; the depth reaches 2, inside "((" and again inside "([".
- At index 4 (the fifth symbol), a ")" that finds the stack empty.
- (2 - 5 + 7) mod 7 = 4 elements.
Practise: 20 questions
FAQs
Why do compilers convert infix to postfix?
Postfix removes the need for brackets and precedence rules during evaluation, so code generation becomes one left-to-right pass with a stack. Many compilers build an expression tree instead, but postfix is exactly that tree's postorder, as shown on tree traversals.
Is the exponent operator always right-associative?
In mathematics and in most textbooks, yes: 2 ^ 3 ^ 2 = 2 ^ 9 = 512. Some languages differ: Python's ** is right-associative, while ^ in C and Java is bitwise XOR, not power. In exam conversions, assume ^ is right-associative unless the question says otherwise.
Why does a circular queue waste one slot?
With only front and rear, an empty queue and a full queue would both have front = rear. Keeping one slot empty makes "full" look different: (rear + 1) mod n = front. Alternatives are a count variable or a boolean full flag, which let all n slots be used.
Where else are queues used in algorithms?
Breadth-first search uses a queue to explore a graph level by level, as traced on graph traversal traces. Operating systems use queues for round-robin CPU scheduling and printer spooling, and network routers use them to buffer packets.
Next step
Work through the 20 application MCQs below, then go back to Linear Data Structures MCQs for arrays, linked lists and the core stack and queue operations. Return to the DSA MCQ hub or try an NSCT mock test.