‹ DS&A interview · Socratic
DSA · Stack · #02

Valid Parentheses

Module 4 · difficulty 2/5·30:00starts on first keystroke

Given a string `s` containing just the characters `'('`, `')'`, `'{'`, `'}'`, `'['` and `']'`, determine if the input string is valid. A string is valid if: - Open brackets are closed by the same type of brackets. - Open brackets are closed in the correct order. - Every close bracket has a corresponding open bracket of the same type.

Examples
  • s = "()" true
  • s = "()[]{}" true
  • s = "(]" false
  • s = "([)]" falseorder matters — close must match the most recent open
  • s = "]" falseclose before any open
Constraints
  • · 1 <= s.length <= 10^4
  • · s consists of parentheses only: ()[]{}
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.