← All questions
MediumCoding
Minimum Number of Swaps to Make the String Balanced
Asked at
ServiceNow
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
- 01Verify equal counts of opening and closing brackets
- 02Scan and maintain unmatched closing count
- 03Increase imbalance when balance goes negative
- 04Use the stated swap operation
- 05Convert maximum imbalance to swaps
Reference answer
Then expect these follow-ups
How would you compute minimum adjacent swaps?
Tests: follow-up reasoning
Why does one arbitrary swap repair two deficit units?
Tests: correctness reasoning
Free to read · better with Enzo
Practice this out loud with Enzo
Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.
Next question