Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional

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

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

Correct answer: B. 4

A stack of indices of unmatched positions gives the length of the current valid run. Counting matched bracket pairs overall instead of a contiguous valid substring gives a different number.

More Coding Interview Problems questions

Exams that ask this topic