Skip to main content

2. Valid Parentheses

easyAsked at Figma

Given a string of brackets, decide whether the sequence is properly nested and matched. Figma asks this because their canvas group/ungroup operations are nested in exactly the same shape — a mismatched open/close in the scene graph corrupts the file.

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

Source citations

Public interview reports confirming this problem appears in Figma loops.

  • Glassdoor (2026-Q1)Figma onsite warm-up, mentioned with the framing 'think of nested frames.'
  • LeetCode Discuss (2025-11)Frequent Figma OA 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

Approaches

1. Repeated string replace

Keep replacing '()', '[]', '{}' with empty string until nothing changes. Empty = valid.

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 one-liner but quadratic. Avoid in interviews.

2. Stack of expected closers

Push the matching closer when you see an opener. On a closer, the top of stack must equal it.

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

Tradeoff: Linear, single pass. The trick is pushing the EXPECTED closer (not the opener) so the comparison is one equality check.

Figma-specific tips

Figma will probably draw the analogy to nested frames or grouped layers on the whiteboard — they want candidates who recognize the recursive/stack shape of nested UI structures. Mention that 'a stack pushes openers and pops on close' before writing, and bonus points if you connect it to how scene-graph traversal undoes its push on the way back up.

Common mistakes

  • Pushing the opener and then doing a lookup at pop time — more code, more bugs. Push the closer.
  • Forgetting the final 'stack must be empty' check — '((' would pass otherwise.
  • Trying to pop from an empty stack on the first closer — guard with 'if (stack.length === 0) return false' or rely on .pop() returning undefined != c.

Follow-up questions

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

  • Longest Valid Parentheses (LC 32) — hard variant.
  • Generate Parentheses (LC 22).
  • Minimum Add to Make Parentheses Valid (LC 921).

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Why push the closer, not the opener?

It removes a lookup at close time. When you see ')' you can compare against the top of stack directly instead of mapping ')' back to '(' and then comparing.

What if the string is empty?

Empty string is technically valid (vacuously matched), and the algorithm naturally returns true since the stack is empty at the end.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →