FastPrepFewest Swaps to Balance Brackets
Problems/Fewest Swaps to Balance Brackets

Fewest Swaps to Balance Brackets

Adobe logoAdobe● MediumINTERNPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: You count the fewest swaps of neighbouring characters that turn a string of round brackets into a balanced one. An Adobe report from a product intern hired through a 2024 on-campus drive (August 2024, selected) lists this as the second coding question of the only interview. They worked in a shared editor that ran built-in tests. The report does not say which swaps are allowed, and it gives no limits or examples.

We chose these rules. A swap exchanges two neighbouring characters. This is our reading. If any two positions could be swapped, the answers would be smaller, and the editorial shows that version too. The string has equally many '(' and ')', and its length is at most 10^5. We wrote new examples.

Our version is about 75% the same as the reported question.

You are given a string s of round brackets with as many '(' as ')'. In one swap you exchange two neighbouring characters.

Return the fewest swaps that make s balanced. A string is balanced when, reading from left to right, the number of ')' read so far never exceeds the number of '(' read so far. Since both counts are equal, a balanced arrangement always exists.

Function

minSwapsToBalance(s: String) → int

Examples

Example 1

s = "())(()"return = 1

The first ) without a partner is at index 2. Swapping it with the ( at index 3 gives ()()(), which is balanced. One swap.

Example 2

s = "(())"return = 0

The string is already balanced, so no swaps are needed.

Example 3

s = "))(("return = 3

Swap indices 1 and 2: )()(. Swap indices 0 and 1: ())(. Swap indices 2 and 3: ()(). Three swaps, and no shorter sequence works.

Constraints

  • 2 <= s.length <= 10^5, and s.length is even
  • s contains only '(' and ')', exactly s.length / 2 of each

More Adobe Phone Screen questions

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
  • Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
  • Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
  • Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
  • Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
  • Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
  • Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
  • Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026

Similar questions at other companies

  • Remove K DigitsProcol · PHONE SCREEN
  • Reorderable Task Scheduler With CooldownWaymo · PHONE SCREEN
  • Reorganize a StringAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Valid Parenthesis StringOracle · PHONE SCREEN
  • Shortest Way to Form a Target StringPinterest · PHONE SCREEN
  • Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEW
  • Minimum Removal for Balanced ParenthesesMeta · PHONE SCREEN · ONSITE INTERVIEW
  • Largest Number Below N from Reusable DigitsByteDance · PHONE SCREEN
  • Longest Dictionary TokenizationGoogle · PHONE SCREEN
  • Minimum Remove to Make Valid ParenthesesBloomberg LP · PHONE SCREEN · ONSITE INTERVIEW

Interview reports that mention this question

  • Adobe Product Intern Interviews: 15 Experiences (Part 3 of 4)
  • Adobe Intern Interview Questions: 54 Experiences (Part 1 of 4)

Adobe interview experiences

  • Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
  • Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
  • Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
  • Adobe New Grad MTS Interview Questions: 7 Experiences
See Adobe hiring insights
Saved
class Solution {
    public int minSwapsToBalance(String s) {
        // Write your code here.
    }
}
Checking account…