Maximum Score in Balanced String
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon's December 2023 OA featured a string problem disguised as a parenthesis-matching game. You're given a string of only parentheses and need to find the maximum score from a balanced substring, where score is the distance between an opening paren and its matching closing paren. The trick is recognizing this isn't really about balance validation, it's about finding the widest valid pair. StealthCoder will pattern-match this instantly if you blank on the approach.
The problem
\ Given a string s consisting of parentheses, you need to find the maximum score possible in a balanced substring of s. The score of a substring is calculated by choosing two indices i and j (0 such that s[i] is an opening parenthesis \"(\" and s[j] is a closing parenthesis \")\". The score of the substring is defined as j - i, i.e., the difference between the indices.\ \ Write a function/method that takes a string s as input and returns the maximum score that can be obtained from a balanced substring of s.\ \ Note\ \ The input string s will only consist of opening and closing parentheses. A balanced substring is a substring that has an equal number of opening and closing parentheses.\ \
Reported by candidates. Source: FastPrep
Pattern and pitfall
The core insight: you want the leftmost opening paren matched with the rightmost closing paren in a valid balanced window. A two-pass or stack-based approach works. First, validate balance as you scan left to right, tracking depth. When depth hits zero, you've found a balanced substring. Record the distance from the earliest unclosed opening to the latest closing in that window. Common trap: overthinking which pair to match. The answer is always the outermost pair in any balanced segment, because that maximizes distance. If you freeze during the OA, StealthCoder will feed you the stack approach: push indices of opens, pop on close, compute max span between opens and closes. It's a single pass, O(n) time.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Maximum Score in Balanced String cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as valid parentheses. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Score in Balanced String FAQ
Is this actually about finding matching parentheses?+
No. It's about finding the widest balanced substring, then measuring the distance from its first opening paren to its last closing paren. You don't match specific pairs; you measure the span. The trick is recognizing balanced means the substring itself is valid, not that every open has a unique close.
Can I solve this without a stack?+
Yes. Two-pointer or running-balance approach works: track net depth as you scan. When depth returns to zero, record the span. The key is marking where each balanced segment starts and ends, then computing distances. Stack is cleaner but not mandatory.
What if the entire string is balanced?+
Then the answer is the length of the string minus one. You're measuring index distance, so from index 0 to index n-1 on a string of length n is distance n-1. Simple case but easy to miss if you're thinking pairs instead of spans.
How do I handle multiple balanced substrings?+
Scan the whole string and track the maximum distance across all balanced segments. If the string is 'by parts, compute each part's span separately and keep the max. Don't assume there's only one.
Will this pattern come up in other Amazon OAs?+
Parenthesis and bracket problems are Amazon staples. Balance validation, matching, and span measurement rotate through their OAs. If you see parens, default to stack or depth-tracking. It's been consistent for years.