Prepin
Log in

What method do you use to identify balanced parentheses in a string? Explain your approach with Python.

Approach / Explanation

The inquiry pertains to how you would tackle the challenge of determining if a string of parentheses is balanced using Python. A balanced set of parentheses means that every opening parenthesis '(' has a corresponding closing parenthesis ')', and they are properly nested and arranged. A widely used technique for this issue is to utilize a data structure known as a stack. You can initialize a stack, and each time you come across an opening parenthesis, you "push" it onto the stack. When you see a closing parenthesis, you will "pop" or remove the most recent parenthesis from the stack, assuming it is an opening one. The string is considered balanced if, after examining all the parentheses in the string, the stack is empty. This method is effective because the last unmatched opening parenthesis should correspond with the next unmatched closing parenthesis. Here are the main points: Initialize a stack data structure. For every parenthesis in the string, push an opening parenthesis onto the stack and pop a closing parenthesis off. Verify that the stack is empty to confirm that the parentheses are balanced.

Suggested Answer

You can begin by creating a stack data structure in Python, which can be represented as a list. Below is a straightforward function that verifies whether the parentheses are balanced: def are_parentheses_balanced ( s ) : stack = [ ] for char in s : if char == '(' : stack . append ( char ) elif char == ')' : if not stack : return False stack . pop ( ) return not stack This function initializes an empty stack and then loops through the input string. When it encounters an opening parenthesis, it appends it to the stack. If it comes across a closing parenthesis, it removes the last element from the stack. If the stack is empty when attempting to pop an element, it indicates that there is a closing parenthesis without a corresponding opening one, resulting in a return of False. Ultimately, if the stack is empty after processing the entire string, it signifies that all parentheses were correctly matched, and the function returns True.

Alternative Answer

Here is another illustration that shows how to manage various types of brackets (not just parentheses): def are_brackets_balanced ( s ) : stack = [ ] bracket_map = { "(" : ")" , "[" : "]" , "{" : "}" } open_brackets = set ( [ "(" , "[" , "{" ] ) for char in s : if char in open_brackets : stack . append ( char ) elif stack and char == bracket_map [ stack [ - 1 ] ] : stack . pop () else : return False return not stack This function, like the previous one, initializes an empty stack along with a mapping of open and closed bracket types, as well as a set for quick access to the open bracket types. It then iterates through each character in the string, checking if it is an open bracket. If it is, the character is added to the stack. When encountering a closing bracket, it checks the top element of the stack to see if it corresponds, and if so, removes it from the stack. If there is a mismatch (or if the stack is already empty), this indicates that there is a closing bracket without a matching opening bracket, and the function returns False. Similarly, if the stack is empty after processing all characters, it returns True, confirming that all brackets were correctly paired.

Question Details

Difficulty & Category

Medium
technical

Companies

Capital One

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.