Optimal Python Solution for Valid Parentheses (Interview Script)
2025-09-30
The Logic
- Parentheses must close in the reverse order they are opened.
- A stack naturally models last-in, first-out behavior.
- Any mismatch or leftover opening bracket invalidates the string.
Implementation / Diagram
Key Invariant
The stack always contains unmatched opening brackets in the order they must be closed.
def isValid(s):
stack = []
pairs = {')': '(', ']': '[', '}': '{'}
for c in s:
if c in pairs:
if not stack or stack[-1] != pairs[c]:
return False
stack.pop()
else:
stack.append(c)
return not stack
⏵ The honest play
You've read the playbook.
Now make sure you pass the live OA.
Knowing the patterns isn't the same as solving them under a timer with a proctor watching. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem on screen and surfaces a working solution in under 2 seconds. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Hedge your live OA
Invisible during screen share