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- A. 2 5 0
- B. 3 4 0
- C. 3 5 0
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? (^ is XOR, & is AND) Integer s = 0, x, p for (each i from 1 to 20) x = i p = 0 while…
- What will be the output of the following pseudocode? Integer P[4][4] = all 0 Integer A[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} for (each…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Function f(Integer n) if (n <…
- What will be the output of the following pseudocode? // BST: smaller keys go to the left, larger to the right Insert into an empty BST, in…
- What will be the output of the following pseudocode? Function ways(Integer n) if (n < 0) return 0 end if if (n == 0) return 1 end if…
- What will be the output of the following pseudocode? (substring(s, i, k) is the k characters starting at index i; / is integer division; +…