Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

What is the length of the longest valid (well-formed) parentheses substring of )()())?

  1. A. 2
  2. B. 4
  3. C. 6
  4. D. 8

Correct answer: B. 4

A stack of indices, with the base index set to the last unmatched ')', measures the longest run of matched pairs. Reporting the total number of matched brackets as the length is the usual mistake.

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

Exams that ask this topic