ORIGINAL CODING QUESTION
Longest balanced action segment
Return the length of the longest contiguous VALID balanced bracket segment encoded as 1=open and -1=close. Balanced totals alone do not ensure valid prefixes.
Original exercise · hard. Editorial role relevance does not imply a company has asked this question.
Input and output
n, then n integer values. Print one integer as described.
Constraints
1 ≤ n ≤ 200; tokens in {1,-1}.
Example 1
Input
5 1 1 -1 -1 1
Output
4
The first four tokens form a valid segment.
Example 2
Input
2 -1 1
Output
0
Equal totals do not form a valid segment.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
An unmatched close resets a candidate segment.
Hint 2
Keep unmatched open indexes and a boundary sentinel.
Hint 3
After a close, the remaining stack boundary determines the valid suffix length.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →