Data Structures & AlgorithmsModerate

What is the length of the longest contiguous substring of "([)][]()[[]]" that is a balanced bracket sequence, where '(' must close with ')' and '[' must close with ']' and brackets nest properly?

  1. A. 8
  2. B. 10
  3. C. 12
  4. D. 7

Correct answer: A. 8

Test each substring with a typed stack: an opening bracket is pushed and a closing bracket must match the top. A balanced block must match type as well as count, so mismatched pairs such as '(]' never count.

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

Exams that ask this topic