Validate Matching Brackets
Updated
Problem statement
Return true if every opening bracket in text is closed by a bracket of the same type and the pairs are properly nested. Otherwise return false.
The supported pairs are (), [] and {}. Every closing bracket must match the most recent opening bracket that is still unclosed. The empty string is valid.
Function
isValidBrackets(text: String) → booleanExamples
Example 1
text = "([{}])"return = trueThe inner braces close first, followed by the square brackets and then the outer parentheses.
Example 2
text = "([)]"return = falseThe first closing parenthesis arrives while an opening square bracket is the most recent unmatched opener, so the types are crossed.
Constraints
- For this exercise, assume
0 <= text.length <= 10000. - Every character is one of
( ) [ ] { }. No other characters or spaces occur.
More JP Morgan Chase Online Assessment questions
- Find Total CostOA · Seen Oct 2026
- Minimum Meeting RoomsOA · Seen Oct 2026
- Count Dropped RequestsOA · Seen Oct 2026
- Maximize Minimum Deployment DifficultyOA · Seen Oct 2026
- Task SchedulingOA · Seen Oct 2026
- Substring RemovalOA · Seen Oct 2026
- Maximum Game Board ScoreOA · Seen Sep 2026
- Minimum Digit Changes Between String HalvesOA · Seen Sep 2026
- Same Substring Within BudgetOA · Seen Sep 2026
- Invert Ad Visibility BitsOA · Seen Aug 2026
Similar questions at other companies
- Validate a Bracket SequenceInMobi · OAInMobi interview questions
- Valid ParenthesesBlackRock · OABlackRock interview questions
- Valid ParenthesesVanguard · OAVanguard interview questions
- Valid Bracket SequenceNVIDIA · OA · PHONE SCREENNVIDIA interview questions
- Get Final StringIBM · OAIBM interview questions
- Backspace String CompareGoldman Sachs · OAGoldman Sachs interview questions
- The PromHackerEarth · OA
- Compare Character Arrays with BackspacesOdoo · OAOdoo interview questions
- Balancing ParenthesesOracle · OAOracle interview questions
- Collapse Equal Adjacent PairsHudson River Trading · OAHudson River Trading interview questions
Interview reports that mention this question
JP Morgan Chase interview experiences
- JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
- JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
- JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
- JP Morgan Chase Data and AI Intern Superday: 11 Experiences
- JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)