Skip to main content

20. Valid Parentheses

easyAsked at Amazon

Given a string of brackets, return true if every opener has a matching, correctly-nested closer. Amazon asks this as a warm-up to test whether you reach for a stack and articulate the LIFO invariant.

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

Source citations

Public interview reports confirming this problem appears in Amazon loops.

  • Glassdoor (2026-Q1)Amazon SDE I phone-screen reports cite this as a recurring warm-up.
  • Blind (2025-12)Recurring Amazon interview question.

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, open brackets are closed in the correct order, and every close bracket has a corresponding open bracket of the same type.

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. Stack of openers (optimal)

Push openers; on closers, check the top of stack matches. Empty stack at end means valid.

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

Tradeoff: Stack is the canonical answer. Linear time, single pass.

Amazon-specific tips

Amazon interviewers want you to name the LIFO property out loud: 'Brackets nest like a stack — the most-recent opener must match the next closer.' That articulation earns the warm-up. Forgetting the final emptiness check (returning true if stack still has openers) is the classic bug.

Common mistakes

  • Forgetting to check stack is empty at the end (a single '(' would return true).
  • Using counters instead of a stack — fails on '([)]' which has correct counts but wrong nesting.
  • Popping without checking if stack is non-empty first.

Follow-up questions

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

  • Minimum Remove to Make Valid Parentheses (LC 1249).
  • Generate Parentheses (LC 22).
  • Longest Valid Parentheses (LC 32).

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Can it be done without a stack?

Not for the multi-bracket variant. Counters work for single-type '()' but fail on '[' and '{' because you need to track WHICH opener is next to close.

What's the most-common bug?

Forgetting the final emptiness check. The interviewer will catch this on '(' alone.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →