ProgrammingHardTCS NQT · Accenture
For the bracket string "(()(()))", the procedure below tracks the nesting depth and prints the maximum depth reached. What is printed?
For the bracket string "(()(()))", the procedure below tracks the nesting depth and prints the maximum depth reached. What is printed?
depth = 0; best = 0
for each character ch
if ch == '(' then depth = depth + 1 else depth = depth - 1
best = max(best, depth)
end for
Print best- A. 3
- B. 4
- C. 8
- D. 0
Correct answer: A. 3
The depth goes 1, 2, 1, 2, 3, 2, 1, 0, so the maximum is 3. Counting the opening brackets gives 4, the length is 8, 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
- Longest common prefix of the words "flower", "flow" and "flight" is found by comparing characters column by column until a mismatch. The…
- The words of "the sky is blue" are reversed in order (the words themselves are kept). The procedure below does this and prints the result…
- The string "bananana" is searched for "ana" with overlapping occurrences allowed. The procedure below counts the start positions of…
- Strings are compressed in place by writing each run of equal characters as the character followed by the run length (a count of 1 is…
- Using Booth-style thinking, the lexicographically smallest rotation of the string "banana" is wanted. The procedure below tries every…
- The word "mississippi" is examined by the procedure below, which counts how many different letters it contains. What is printed? seen =…