Valid Parentheses (Balanced Brackets)

easy
By FrontendAtlas Editorial · Updated Oct 19, 2026
Implement isValidParentheses(s): return true when every (, [ and { in the string is closed by the matching bracket in the right order, and false otherwise. Characters that are not brackets are ignored, an empty string is valid, and input that is not a string throws a TypeError. Three public interview reports of Walmart frontend and UI loops (October 2020, January 2024 and November 2024) list Valid Parentheses in a coding or DSA round; this practice version adds the rule for non-bracket characters so the same check works on code snippets. Concepts: strings, data-structures, algorithms, validation.

What you'll practice:

  • Matching every opener to its closer with a stack of expected closers
  • Failing fast on the first closer that does not match
  • Stating the contract for empty input, other characters and non-strings before coding

Expected behavior

Your isValidParentheses() implementation should:

  • Return true when every opener is closed by the same kind of bracket in the right order
  • Return false for a closer of the wrong kind, such as "(]", or a wrong closing order, such as "([)]"
  • Return false when an opener is never closed or a closer arrives with nothing open
  • Treat an empty string as valid
  • Ignore every character that is not one of ( ) [ ] { }
  • Throw a TypeError when the input is not a string

Arguments

  • s: string — The text to check. Only ( ) [ ] { } count; every other character is ignored.

Returns

boolean — true when the brackets are balanced and properly nested, otherwise false.

Solution idea

Interview answer: scan the string once and keep a stack of the closers you still expect. Each opener pushes the closer it needs; each closer pops the top of the stack and must equal it. A mismatch, or a closer that finds the stack empty, means the string is invalid, and the string is valid only when the stack is empty at the end. Other characters are skipped. Time O(n), memory O(n) in the worst case of all openers.

Examples
isValidParentheses('()[]{}'); // => true
isValidParentheses('{[()]}'); // => true
isValidParentheses('([)]'); // => false (closed in the wrong order)

isValidParentheses('fn(a[0], { b: 1 })'); // => true (other characters are ignored)
isValidParentheses('('); // => false (never closed)
isValidParentheses(''); // => true

Public interview reports

These public, self-reported interview accounts informed this practice version. The exercise is modelled on reported rounds, not copied from them, and the reports are not verified official questions or endorsements.