As reported: Amazon SDE2 loop (May 2025), DSA round — Longest Valid Parentheses (LeetCode 32) and a problem similar to Dungeon Game.
This is LeetCode 32 — Longest Valid Parentheses.
Given a string s containing only '(' and ')', return the length of the longest substring that is well-formed (every opening bracket is closed in the right order).
Example 1
s = "(()"
Output: 2 ("()")
Example 2
s = ")()())"
Output: 4 ("()()")
Constraints: 0 ≤ s.length ≤ 3·10⁴; s[i] is '(' or ')'.
Code runner format: LeetCode's test-case format — s as a JSON string. Implement longestValidParentheses(s); the harness prints the length.