Valid Parenthesis String
Reported by candidates from Motive's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Each '*' in the string can be an opening paren, a closing paren, or nothing, and Motive wants to know if the whole thing can balance. That's the Valid Parenthesis String problem, reported in January 2026. It looks like a string problem, but it's really a greedy counting problem in disguise. If you're taking this OA in the next couple of days, you need one idea and you need it cold. The brute force branches three ways per star and dies on a 10^5 length input. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment.
The problem
Given a string s containing '(', ')', and '*', return whether it can become a valid parenthesis string.
Each '*' may independently represent an opening parenthesis, a closing parenthesis, or the empty string.
Function
checkValidString(s: String) → boolean
Examples
Example 1
s = "(*)"
return = true
The wildcard may be empty.
Example 2
s = "(*))"
return = true
Treat the wildcard as an opening parenthesis.
Constraints
0 <= s.length <= 10^5.
s contains only (, ), and *.Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is tracking a range instead of a single count. Keep two numbers, lo and hi, the minimum and maximum possible count of unmatched open parens. For '(' increment both. For ')' decrement both. For '*' decrement lo (treat as close) and increment hi (treat as open). If hi ever goes below 0, return false, since even the best case has too many closers. Clamp lo at 0 after each step, because you can't have negative open parens. At the end, return lo == 0. The common pitfall is forgetting the clamp, which makes valid strings like "*(" fail wrongly. Another is using a single counter and guessing star roles. It's O(n) time and O(1) space. If you blank on the range idea during the live OA, StealthCoder can surface the lo/hi solution so you can verify it against the examples and move on.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Valid Parenthesis 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 parenthesis string. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Motive's OA.
Motive 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.
Valid Parenthesis String FAQ
What's the trick for Valid Parenthesis String?+
Track a range of possible open-paren counts, lo and hi. Stars widen the range: lo goes down, hi goes up. If hi drops below zero, fail. Clamp lo at zero. At the end, the string is valid if lo is zero. One pass, constant space.
How hard is this problem really?+
It's rated medium, but it feels hard if you've never seen the greedy range idea. Once you know it, the code is about ten lines. The difficulty is the insight, not the implementation. Most candidates who fail try recursion and time out.
Can I solve it with dynamic programming or recursion?+
Yes, but it's slower. Recursion with memoization or an interval DP works, yet the input can reach 10^5 characters, so O(n^2) or O(n^3) approaches will be too heavy. The greedy O(n) approach is what you want to write.
What edge cases should I test?+
Test the empty string, which is valid. Test "*(", which should be false, and "(*", which should be true. Also try all stars, and a string starting with ')'. The clamp on lo is what the "*(" case checks.
How do I prepare in 48 hours?+
Write the lo/hi solution from memory twice, then trace it by hand on "(*))" and "*(". Also do a two-stack version, one for parens and one for stars, as a backup. Understand why hi below zero means failure, and you're set.