ProgrammingHardAmazon OA · Goldman Sachs OA

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
String s = "ABBACA"
Stack st = empty
for (each ch in s)
    if (st not empty AND top(st) == ch)
        pop(st)
    else
        push(st, ch)
    end if
end for
Print stack contents from bottom to top
  1. A. AACA
  2. B. CA
  3. C. ACA
  4. D. BBCA

Correct answer: B. CA

Equal neighbours cancel, and cancellations can chain: B B cancel, which brings A next to A, which also cancel. What is left is CA.

More Pseudocode and Code Tracing questions

Exams that ask this topic