Data Structures & AlgorithmsModerate

How many contiguous substrings of "(()())(())((()))" are balanced round-bracket sequences whose maximum nesting depth is at most 2?

  1. A. 37
  2. B. 9
  3. C. 4
  4. D. 10

Correct answer: B. 9

Track the running depth; a block is balanced when it ends at zero and never goes negative, and its peak depth must not exceed two. Counting any equal-count block ignores the prefix condition, and only length-2 blocks is a narrower subset.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic