Stack and Queue Applications: Worked Traces for NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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

  1. Name the structure. Most-recent-first problems (brackets, expressions, undo) use a stack; first-come-first-served problems (scheduling, buffering, BFS) use a queue.
  2. Draw a three-column table: input symbol, stack or queue state, output.
  3. Write the precedence ladder before converting: ^ (3, right-associative), * and / (2), + and - (1, both left-associative).
  4. Process one symbol per row. Never do two pops in your head.
  5. 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:

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).

  1. Inner pieces: A - B becomes - A B, and D + E becomes + D E.
  2. Division: / (- A B) C.
  3. 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

Common mistakes

Practice set

These are separate from the quiz below.

  1. Convert A + B - C * D to postfix.
  2. Convert (A + B) * (C - D) ^ E to postfix.
  3. Convert A / B ^ C - D to prefix.
  4. Evaluate the postfix 6 3 / 4 2 - *.
  5. Evaluate the postfix 9 2 2 ^ ^.
  6. Evaluate the prefix * - 8 3 + 1 2.
  7. Is ((a+b)*[c-d]) balanced, and what is its maximum stack depth?
  8. Where does the checker first fail on (())) ?
  9. A circular queue of size 7 (one slot kept empty) has front = 5 and rear = 2. How many elements does it hold?
Show answers
  1. A B + C D * -. The + is popped when - arrives (equal precedence, left-associative).
  2. 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.
    1. 6 / 3 = 2, 4 - 2 = 2, and 2 * 2 = 4.
    1. 2 ^ 2 = 4, then 9 ^ 4 = 6561; the right-associative grouping is built in.
    1. (8 - 3) * (1 + 2) = 5 * 3.
  3. Yes; the depth reaches 2, inside "((" and again inside "([".
  4. At index 4 (the fifth symbol), a ")" that finds the stack empty.
  5. (2 - 5 + 7) mod 7 = 4 elements.

Practise: 20 questions

Interactive quiz loading. The first 10 questions are shown below.

  1. When converting an infix expression to postfix with the stack (shunting-yard) method, what happens to each operand as it is read?

    1. It is written straight to the output
    2. It is pushed onto the operator stack
    3. It is held until the matching closing bracket
    4. It is compared with the operator on top of the stack
    Show answer

    Answer: A. It is written straight to the output

    Operands keep their left-to-right order in postfix, so they go directly to the output. Only operators and opening brackets ever wait on the stack.

  2. In infix-to-postfix conversion, an incoming operator with the SAME precedence as the stack top normally pops it first. Why is ^ (power) an exception that is pushed without popping another ^?

    1. ^ has the lowest precedence of all operators
    2. ^ is right-associative, so A ^ B ^ C means A ^ (B ^ C)
    3. ^ only appears inside brackets
    4. ^ is a unary operator
    Show answer

    Answer: B. ^ is right-associative, so A ^ B ^ C means A ^ (B ^ C)

    Left-associative operators such as - and / group from the left, so the earlier one must be output first. Exponentiation groups from the right, so the later ^ must be applied first; keeping the earlier ^ on the stack achieves that, giving ABC^^.

  3. A stack-based bracket checker has scanned the whole string without ever finding a mismatched or unmatched closing bracket. When is the string balanced?

    1. Always, because no mismatch was found
    2. Only if the stack holds exactly one bracket
    3. Only if the stack is now empty
    4. Only if the string has an even length
    Show answer

    Answer: C. Only if the stack is now empty

    Brackets that were opened but never closed are still on the stack at the end, for example in "(()". So the final check is that the stack is empty. Even length alone proves nothing: ")(" has even length.

  4. Why is postfix notation convenient for a computer to evaluate?

    1. It is shorter than the infix form of every expression
    2. It can be evaluated without any memory at all
    3. It always evaluates to the same value as reading the infix left to right
    4. It needs no brackets or precedence rules and is evaluated left to right with one stack
    Show answer

    Answer: D. It needs no brackets or precedence rules and is evaluated left to right with one stack

    In postfix the order of operations is built into the token order. A single left-to-right scan pushes operands and applies each operator to the top two values, with no precedence table and no brackets.

  5. A circular queue in an array of size 8 keeps one slot permanently empty so that "full" and "empty" can be told apart using only front and rear. How many elements can it hold at most?

    1. 8
    2. 4
    3. 7
    4. 6
    Show answer

    Answer: C. 7

    With front = rear meaning empty, the queue is declared full when (rear + 1) mod n = front, which leaves one slot unused. So an array of size 8 holds at most 7 elements. Keeping a separate count variable lets all 8 slots be used.

  6. Which of these is a natural application of a QUEUE rather than a stack?

    1. Sending jobs to a shared printer in arrival order
    2. Undoing the last edit in a text editor
    3. Checking that brackets are balanced
    4. Returning from nested function calls
    Show answer

    Answer: A. Sending jobs to a shared printer in arrival order

    A print spooler must serve jobs first-come first-served, which is FIFO, a queue. Undo, bracket matching and function returns all need the most recent item first, which is LIFO, a stack.

  7. Convert the infix expression A * (B + C) / D - E to postfix.

    1. A B C + D / * E -
    2. A B C * + D / E -
    3. A B + C * D / E -
    4. A B C + * D / E -
    Show answer

    Answer: D. A B C + * D / E -

    Inside the brackets B + C becomes B C +. Then * and / have equal precedence and are left-associative, so A (B C +) * comes first, then D /, and finally E -. Result: A B C + * D / E -.

  8. Convert the infix expression (A + B) * C - D ^ E ^ F to postfix, treating ^ as right-associative with the highest precedence.

    1. A B + C * D E ^ F ^ -
    2. A B + C * D E F ^ ^ -
    3. A B + C D E F ^ ^ - *
    4. A B C + * D E F ^ ^ -
    Show answer

    Answer: B. A B + C * D E F ^ ^ -

    (A + B) gives A B +, then * C gives A B + C *. When - arrives, * is popped. The two ^ operators stack up without popping each other because ^ is right-associative, so D E F ^ ^ is output at the end, followed by -.

  9. What is the prefix (Polish) form of (P + Q) * (R - S) / T?

    1. * + P Q / - R S T
    2. / * + P Q - R S T
    3. / * P Q + - R S T
    4. P Q + R S - * T /
    Show answer

    Answer: B. / * + P Q - R S T

    Multiplication and division are left-associative, so the expression is ((P + Q) * (R - S)) / T. Prefix puts each operator before its operands: / then * + P Q - R S, then T. Result: / * + P Q - R S T. The last option is the postfix form.

  10. Evaluate the postfix expression 5 3 + 2 * 8 4 / - using a stack.

    1. 18
    2. 12
    3. -14
    4. 14
    Show answer

    Answer: D. 14

    5 3 + gives 8; 8 2 * gives 16; 8 4 / gives 2; finally 16 2 - gives 14. Remember the first value popped is the right operand.

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.