ProgrammingVery hardLTIMindtree · Cognizant GenC

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
String s = "{[()]}(()"
Stack st
Integer pops = 0, i
Boolean ok = true
for (i = 0; i < length(s); i = i + 1)
    if (s[i] is an opening bracket)
        push s[i]
    else if (st not empty AND top(st) matches s[i])
        pop
        pops = pops + 1
    else
        ok = false
        break
    end if
end for
if (ok AND st is empty)
    Print "YES", pops, size(st)
else
    Print "NO", pops, size(st)
end if
  1. A. YES 4 0
  2. B. NO 3 1
  3. C. NO 4 1
  4. D. NO 4 0

Correct answer: C. NO 4 1

The first six characters match fully (3 pops); then '(' '(' ')' pop one more, leaving one '(' unmatched: 4 pops and 1 element left, so the result is NO 4 1.

More Pseudocode and Code Tracing questions

Exams that ask this topic