Skip to main content

2. Valid Parentheses

easyAsked at Plaid

Determine if a string of brackets is balanced and properly nested. Plaid uses this to test stack reflexes before moving to nested JSON validation in webhook payloads.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • Glassdoor (2025-Q4)Plaid platform-team phone screen warm-up.
  • LeetCode Discuss (2026)Common Plaid intro problem.

Problem

Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if open brackets are closed by the same type of brackets and open brackets are closed in the correct order.

Constraints

  • 1 <= s.length <= 10^4
  • s consists of parentheses only '()[]{}'.

Examples

Example 1

Input
s = "()"
Output
true

Example 2

Input
s = "()[]{}"
Output
true

Example 3

Input
s = "(]"
Output
false

Explanation: Mismatched closer.

Approaches

1. Replace pairs repeatedly

Keep removing '()', '[]', '{}' until no more replacements happen, then check if empty.

Time
O(n^2)
Space
O(n)
function isValid(s) {
  let prev;
  while (prev !== s) {
    prev = s;
    s = s.replace('()', '').replace('[]', '').replace('{}', '');
  }
  return s === '';
}

Tradeoff: Cute but O(n^2). Don't ship this — it shows you didn't reach for a stack.

2. Stack

Push openers, pop and match closers. Empty stack at the end means balanced.

Time
O(n)
Space
O(n)
function isValid(s) {
  const pair = { ')': '(', ']': '[', '}': '{' };
  const stack = [];
  for (const ch of s) {
    if (ch in pair) {
      if (stack.pop() !== pair[ch]) return false;
    } else {
      stack.push(ch);
    }
  }
  return stack.length === 0;
}

Tradeoff: Linear time, single pass. The map-of-pairs makes the matching obvious to a reader.

Plaid-specific tips

Plaid loves seeing you treat brackets as a tiny grammar — that primes you for parsing webhook JSON later in the loop. Bonus signal: explicitly mention what happens with an unclosed opener (stack non-empty) and an early closer (stack empty when you pop).

Common mistakes

  • Forgetting to check stack.length === 0 at the end — '(((' passes if you only check pops.
  • Treating ')' as an opener because of a missing 'else' branch.
  • Using includes() or indexOf() on the stack — linear lookup turns this O(n^2).

Follow-up questions

An interviewer at Plaid may pivot to one of these next:

  • Allow wildcards like '*' that can match any bracket.
  • Return the position of the first invalid character.
  • Stream version: brackets arrive one at a time — same algorithm, one pointer instead of an iterator.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Does a single character ever return true?

No — a lone opener leaves a non-empty stack and a lone closer pops an empty stack. Both correctly return false.

Why use a map instead of three if-statements?

Easier to read and easier to extend if a follow-up adds angle brackets. Plaid's style is tabular — they like data-driven branching.

Practice these live with InterviewChamp.AI

Drill Valid Parentheses and other Plaid interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →