ProgrammingHardTCS NQT · Accenture
The procedure below checks the bracket string "(()())" and prints the maximum nesting depth it reaches. The depth is raised by '(' and lowered by ')'. What is printed?
The procedure below checks the bracket string "(()())" and prints the maximum nesting depth it reaches. The depth is raised by '(' and lowered by ')'. What is printed?
depth = 0; best = 0
for each character ch
depth = depth + 1 for '(' and depth = depth - 1 for ')'
best = max(best, depth)
end for
Print best- A. 3
- B. 6
- C. 0
- D. 2
Correct answer: D. 2
The depth goes 1, 2, 1, 2, 1, 0, so the maximum is 2. Counting the opening brackets gives 3, the length is 6, and the final depth after the scan is 0.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The sentence "to be or not to be to" is split into words, and the procedure below prints the most frequent word (ties go to the first word…
- The string "aabbc" is examined by the procedure below, which counts the substrings (contiguous pieces, counted by position) that contain…
- The procedure below counts the positions i in "abbab" where the two-letter window s[i]s[i+1] is "ab" or "ba". What is printed? count = 0…
- The procedure below counts the positions i in "mississippi" where the two letters s[i]s[i+1] are both "s" (overlaps allowed, though "ss"…
- The palindromic substrings of length exactly 3 in the string "abacaba" are counted by the procedure below, which checks each window of…
- The number of distinct substrings (non-empty, counted once per distinct text) of the string "aab" is computed by the procedure below using…