ProgrammingModerateAmazon OA · Capgemini

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
String s = "(()(()))()"
Integer depth = 0, best = 0, pairs = 0
for (each i from 0 to length(s) - 1)
    if (s[i] == '(')
        depth = depth + 1
        if (depth > best)
            best = depth
        end if
    else
        depth = depth - 1
        pairs = pairs + 1
    end if
end for
Print best, pairs, depth
  1. A. 2 5 0
  2. B. 3 4 0
  3. C. 3 5 0
  4. D. 4 5 0

Correct answer: C. 3 5 0

Depth goes 1, 2, 1, 2, 3, 2, 1, 0, 1, 0, so the maximum nesting is 3; there are 5 closing brackets and the string is balanced: 3 5 0.

More Pseudocode and Code Tracing questions

Exams that ask this topic