Data Structures & AlgorithmsVery hardICPC regional · Amazon SDE OA

What is the length of the longest well-formed (balanced) contiguous substring of ")()())"?

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

Correct answer: C. 4

A stack that stores indices of unmatched ')' gives the longest valid run as current index minus stack top. The string contains the balanced block "()()" of length 4 after the leading ')'.

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

Exams that ask this topic