Balanced Parentheses Inside a String
Updated
Problem statement
The string s holds letters, digits and round parentheses. Ignore everything except the parentheses. Return true if every ( can be paired with a later ) and every ) with an earlier (, with pairs nested or side by side but never crossing. Otherwise return false.
A string with no parentheses is balanced.
Function
isBalanced(s: String) → booleanExamples
Example 1
s = "ab(c(d)e)f"return = trueLetters are ignored. The inner (d) closes first, then the outer pair closes around it, so every parenthesis has a partner.
Example 2
s = "((x)"return = falseTwo parentheses open but only one closes, so one is left without a partner.
Example 3
s = "1)(2"return = falseThere is one of each, but the ) comes first, when nothing is open yet, so it cannot be matched.
Example 4
s = "abc"return = trueWith no parentheses at all there is nothing to match, so the string counts as balanced.
Constraints
1 <= s.length <= 10^5scontains only English letters, digits,(and).
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- Valid ParenthesesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Valid ParenthesesEPAM · ONSITE INTERVIEWEPAM interview questions
- Valid ParenthesesQualys · ONSITE INTERVIEWQualys interview questions
- Rich Text Parser Part 1 (Validate Tokens)Figma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Valid ParenthesesAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Valid ParenthesesKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Valid ParenthesesSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Minimum Parenthesis Removals and Their IndicesDoorDash · ONSITE INTERVIEWDoorDash interview questions
- Valid ParenthesesBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Maximum Parenthesis Nesting DepthRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences