ProgrammingHardAmazon OA · TCS Digital

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
String in = "A+B*C-D/E", out = ""
Stack st = empty
// prec: * and / are 2, + and - are 1
for (each ch in in)
    if (ch is a letter)
        out = out + ch
    else
        while (st not empty AND prec(top(st)) >= prec(ch))
            out = out + pop(st)
        end while
        push(st, ch)
    end if
end for
while (st not empty)
    out = out + pop(st)
end while
Print out
  1. A. ABC*+DE/-
  2. B. AB+C*DE/-
  3. C. ABC*+D-E/
  4. D. ABCDE*/+-

Correct answer: A. ABC*+DE/-

This is infix-to-postfix conversion with left-to-right associativity. B*C is emitted before + leaves the stack; when - arrives it pops * and +; then D/E follows. Result: ABC*+DE/-.

More Pseudocode and Code Tracing questions

Exams that ask this topic