Skip to main content

20. Valid Parentheses

easyAsked at AMD

Given a string of brackets, determine if it is valid. AMD uses this to test stack fluency — knowing when a LIFO structure is the right tool is fundamental to compiler and parser design, both core to AMD's toolchain work.

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

Source citations

Public interview reports confirming this problem appears in AMD loops.

  • Glassdoor (2025-12)AMD SWE new-grad interviewers list Valid Parentheses among common easy-round warm-ups.
  • Blind (2025-10)AMD interview prep threads flag this as a stack-fundamentals check in phone screens.

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 must be closed by the same type of brackets, open brackets must be 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

Explanation: Single matching pair.

Example 2

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

Explanation: All pairs correctly matched and ordered.

Example 3

Input
s = "(]"
Output
false

Explanation: Mismatched types.

Approaches

1. Stack

Push open brackets onto a stack. For each closing bracket, check whether it matches the top of the stack. If the stack is empty at a close or the types mismatch, return false. Valid if the stack is empty at the end.

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

Tradeoff: O(n) time, O(n) worst-case space (all opens before any close). This is the canonical solution. The closing-bracket map is cleaner than a chain of if-else checks.

AMD-specific tips

AMD compiler and toolchain engineers deal with nested structures constantly — expression trees, LLVM IR brackets, ISA encoding parentheses. Frame your answer in those terms: 'a stack is the natural model for any nested matching problem because it enforces LIFO order.' Use the closing-bracket map idiom — it's concise and shows you understand the symmetry of the problem.

Common mistakes

  • Returning true when the stack is non-empty at the end — unmatched open brackets make the string invalid.
  • Calling stack.pop() without checking if the stack is empty first can cause an undefined match with map[ch].
  • Using a counter instead of a stack — a counter fails on mixed bracket types like '([)]'.
  • Forgetting that the empty string is valid — the loop simply doesn't execute and the stack remains empty.

Follow-up questions

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

  • Generate Parentheses (LC 22) — generate all combinations of n pairs of valid parentheses.
  • Longest Valid Parentheses (LC 32) — find the longest valid substring.
  • How would you validate a bracket sequence as a streaming parser with bounded memory?

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 does a counter fail for mixed brackets?

A counter only tracks depth, not type. '([)]' has balanced depth but mismatched types; a stack catches this because it remembers which bracket is open.

What is the space complexity in the best case?

O(1) if the string alternates open-close correctly (stack never grows beyond 1). Worst case O(n) if all opens come first.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →