← All questions
MediumCoding

Minimum Number of Swaps to Make the String Balanced

Asked atServiceNow
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+
  1. 01Verify equal counts of opening and closing brackets
  2. 02Scan and maintain unmatched closing count
  3. 03Increase imbalance when balance goes negative
  4. 04Use the stated swap operation
  5. 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

Hiring now

ServiceNow has 794 open roles right now

landed applies to roles like these for you, on each company’s own site, with answers written for the role.

Get 5x interviews10 free auto-applies. No card.
  • Customer Success Executive

    Remote

  • CRM Senior Solution Sales Executive

    Brasilia, Brasilia, Brazil

  • CPQ Senior Solution Sales Executive

    Sao Paulo, SAO PAULO, Brazil

Or practice this question first

Next question