Valid Parentheses
Updated
Problem statement
You are given a string s made only of the characters (, ), [, ], { and }. Return true if the brackets are valid, and false otherwise.
The brackets are valid when:
- every opening bracket is closed by a bracket of the same type,
- brackets are closed in the correct order, so the most recently opened bracket closes first, and
- every closing bracket has a matching opening bracket before it.
Function
isValid(s: String) → booleanExamples
Example 1
s = "{[()]}"return = trueThe ( closes first, then [, then {. Each closes the most recent open bracket of the same type, so the string is valid.
Example 2
s = "([)]"return = falseWhen ) arrives, the most recent open bracket is [, not (. The order is wrong, so the string is not valid.
Example 3
s = "(()"return = falseThe first ( is never closed, so the string is not valid.
Constraints
1 <= s.length <= 10^4scontains only()[]{}.
More Cisco interview questions
- Check if a String Is a PalindromePHONE SCREEN · Seen Nov 2026
- Find Unique ElementsPHONE SCREEN · Seen Nov 2026
- First Letter to Appear TwicePHONE SCREEN · Seen Nov 2026
- Middle of a Linked ListPHONE SCREEN · Seen Nov 2026
- Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
- Reverse a StringPHONE SCREEN · Seen Nov 2026
- K-Step Bit Flip with RetentionPHONE SCREEN · Seen Nov 2026
- Longest Non-Repeating SubstringPHONE SCREEN · Seen Jul 2026
- Todo Task Manager OperationsONSITE INTERVIEW · Seen Oct 2026
- Find the Duplicate Number Without Modifying the ArrayONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Valid ParenthesesByteDance · PHONE SCREENByteDance interview questions
- Stack Formula InterpreterTypeSafe AI · PHONE SCREEN
- Validate a Bracket SequenceTripAdvisor · PHONE SCREEN
- Rich Text Parser Part 1 (Validate Tokens)Figma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Valid ParenthesesOracle · PHONE SCREENOracle interview questions
- Valid ParenthesesAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Balanced Bracket String CheckRippling · PHONE SCREENRippling interview questions
- Valid Bracket SequenceNVIDIA · OA · PHONE SCREENNVIDIA interview questions
- Valid ParenthesesBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Valid ParenthesesPeopleStrong · PHONE SCREENPeopleStrong interview questions