Check if a Parentheses String Can Be Valid
A medium-tier problem at 45% community acceptance, tagged with String, Stack, Greedy. Reported in interviews at ServiceNow and 0 others.
You get a string with parentheses and wildcards, and you need to decide if it can be made valid by swapping wildcards for either '(' or ')'. ServiceNow has asked this. It's a medium-difficulty string problem that catches candidates off guard because the greedy trick isn't obvious on first read. You might assume a single pass is enough, then realize you need to validate from both directions. If this hits your live assessment and you blank on the two-pass strategy, StealthCoder solves it in seconds, invisible to the proctor.
Companies that ask "Check if a Parentheses String Can Be Valid"
Check if a Parentheses String Can Be Valid is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share.
Get StealthCoderThe trap is thinking a single left-to-right scan works. It doesn't. You need to greedily match '(' with ')' and wildcards from left to right, then do the same from right to left to catch unmatched '(' characters. Track open parentheses and wildcards as you go. If at any point you run out of both before closing a ')', that direction fails. The key insight is that wildcards are flexible, so you consume them as needed to keep the string balanced. Common failure: not checking the right-to-left direction, which leaves dangling open parens undetected. This is a greedy plus string manipulation problem that rewards structured thinking. StealthCoder is your hedge if you get stuck during the assessment and can't quite nail the two-pass logic.
Pattern tags
You know the problem.
Make sure you actually pass it.
Check if a Parentheses String Can Be Valid recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Check if a Parentheses String Can Be Valid interview FAQ
Is this really asked at big companies?+
ServiceNow has confirmed this problem. It's medium difficulty with a 44% acceptance rate, so it's not trivial but not a rare edge case either. Expect it if you're interviewing for roles that screen on string and stack fundamentals.
What's the trick that most people miss?+
One pass left-to-right isn't enough. You must validate right-to-left as well to catch unmatched open parentheses. Wildcards are the wild card, and you need to greedily spend them in both directions. Miss the second pass and you'll get wrong answers on test cases with trailing '('.
Is this a stack problem or greedy?+
It leans greedy more than stack. You don't need to build a stack; you track counts of open parens and available wildcards and consume them as you iterate. Stack is intuitive for some, but the greedy two-pass approach is cleaner and more efficient here.
How long should this take to code?+
If you know the pattern, 5 to 10 minutes. If you're discovering it live, 15 to 25 minutes. The logic is simple once you see the two-pass structure. Test case coverage is the real time sink, not the algorithm itself.
Will I fail if I use a stack instead of greedy?+
No, but it's slower and wastes space. Stack solutions work if coded carefully, but greedy is O(n) time and O(1) space. In a live assessment, greedy is the safer, faster approach if you can nail the two-pass logic.
Want the actual problem statement? View "Check if a Parentheses String Can Be Valid" on LeetCode →