Prepin
Log in

Find the least number of parentheses required to balance a specified string.

Approach / Explanation

This question requires you to determine the least number of parentheses necessary to balance a given string. Balancing a string of parentheses means that every opening parenthesis must have a corresponding closing parenthesis and vice versa. For instance, if the input string is '((((' , it needs four closing parentheses to achieve balance. Therefore, the output will be 4. To tackle this question, you will likely implement an algorithm to traverse the string, count the unmatched opening and closing parentheses, and combine these counts. Here are the important points to keep in mind while answering: You will probably need to go through each character in the string to determine whether it is an opening or closing parenthesis. You will require a method to keep track of unmatched parentheses. The sum of unmatched opening and closing parentheses will give you the answer – the minimum number of parentheses needed to balance the string.

Suggested Answer

Let's examine the input string ')))((' . We will go through the string one character at a time. For the first character, ')', it is a closing parenthesis that has no corresponding opening parenthesis preceding it, so we maintain a count of 1. The second character is also a closing parenthesis without a matching opening parenthesis, which brings our count to 2. The third character is yet another closing parenthesis, again lacking a corresponding opening parenthesis, so we increase our count to 3. Upon reaching the fourth character, we encounter an opening parenthesis. However, instead of finding a matching closing parenthesis, we come across another opening parenthesis. Thus, we add 1 to our count. As a result, we arrive at a total count of 4, which is our final outcome. The input string ')))((' requires at least 4 parentheses to achieve balance.

Alternative Answer

Let's take another example: the input string is '(()))' . As we go through the string, the first character is an opening parenthesis. If there isn't a corresponding closing parenthesis after it, we need to increase our count by 1. The second character, however, is a closing parenthesis that pairs with the previous opening parenthesis, so we don't need to adjust the count for these two. Moving on to the third character, we encounter another opening parenthesis, and similarly, the fourth character is a matching closing parenthesis. We also don't need to add to our count for these. When we reach the fifth character, it is a closing parenthesis without an associated opening parenthesis. Therefore, we add 1 to our count. In conclusion, the input string '(()))' needs at least 1 extra parenthesis to become balanced.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Meta (Facebook)

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

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