Balanced Brackets
Updated
Problem statement
You are given a string s made only of the bracket characters '(', ')', '[', ']', '{' and '}'. Decide whether its brackets are balanced.
The string is balanced when all of these hold:
- Every opening bracket is closed by a closing bracket of the same type:
'('by')','['by']', and'{'by'}'. - Brackets close in the right order: a closing bracket always closes the most recent opening bracket that is still open.
- Every closing bracket has an opening bracket before it to close.
Return true if s is balanced, and false otherwise.
Function
isValid(s: String) → booleanExamples
Example 1
s = "{[()]}()"return = trueThe '(' at index 2 is closed right away by the ')' at index 3. Then ']' closes the '[' at index 1, and '}' closes the '{' at index 0. The last two characters form their own pair. Every bracket is closed by the same type in the right order, so the string is balanced.
Example 2
s = "[(])"return = falseWhen the ']' at index 2 arrives, the most recent bracket that is still open is the '(' at index 1. A ']' cannot close a '(', so the pairs cross and the string is not balanced, even though each opening bracket has a closing bracket of the same type later in the string.
Example 3
s = "(()"return = falseThe ')' at index 2 closes the '(' at index 1. The '(' at index 0 is never closed, so the string is not balanced.
Constraints
1 <= s.length <= 104scontains only the characters'(',')','[',']','{'and'}'.
More Airbnb Phone Screen questions
- Read Across Sound TracksPHONE SCREEN · Seen Nov 2026
- Text JustificationPHONE SCREEN · Seen Nov 2026
- Decode and Store StringsPHONE SCREEN · Seen Oct 2026
- Print Sentences as TablePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Detonate Bombs with Chain ReactionsPHONE SCREEN · Seen Aug 2026
- Smallest Common RegionPHONE SCREEN · Seen Aug 2026
- Longest Chain BookingPHONE SCREEN · Seen Aug 2026
- Nested List Iterator with RemovePHONE SCREEN · Seen Jul 2026
- Parse Query StringPHONE SCREEN · Seen May 2026
- Pacific Atlantic Water FlowPHONE SCREEN · Seen May 2023
Similar questions at other companies
- Valid ParenthesesByteDance · PHONE SCREENByteDance interview questions
- Valid ParenthesesCisco · PHONE SCREENCisco 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
Interview reports that mention this question
Airbnb interview experiences
- Airbnb SWE OA: Four-Level Banking System: 1 Experience
- Airbnb Campus and Full-Time SWE OA / Interviews: 9 Experiences
- Airbnb Intern Phone Interviews: 21 Experiences (Part 7 of 7)
- Airbnb Intern OA and Phone Screens: 17 Experiences (Part 6 of 7)
- Airbnb Intern OA, Diplomacy Game: 19 Experiences (Part 4 of 7)