Longest Well-Formed Bracket Span in a Corrupted Config File
Last updated: November 29, 2026
Quick Overview
A config file was partially corrupted, leaving a string of '(' and ')' characters. Return the length of the longest contiguous substring that is a well-formed bracket sequence.
0
0
0 solved
A config file was partially corrupted, leaving a string of '(' and ')' characters. Return the length of the longest contiguous substring that is a well-formed bracket sequence.
A Google onsite hard where a 1D DP indexed by the end position needs careful indexing to chain nested and adjacent groups. Interviewers often accept a stack solution but probe the DP to see whether you can define dp[i] precisely.
What the Interviewer Expects
- Define best[i] as the longest valid substring ending exactly at i
- Handle the '()' case and the '))' case separately
- Chain an earlier adjacent valid block in the '))' case
- Avoid out-of-range indices
- Mention the stack and two-counter alternatives
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Solve it with O(1) extra space.
- Support three bracket types.
- Return the start and end of the longest span.
- Count the number of valid substrings instead.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: string s of '(' and ')'. Output the max length of a contiguous substring that is balanced and never dips below zero. Empty string returns 0....
High-Level Design
Let best[i] be the length of the longest valid substring ending at index i, or 0. Only ')' can end one. If s[i-1] == '(', then best[i] = best[i-2] + 2 (a fresh pair glued onto whatever valid blo...