FastPrepMinimum Swaps Required to Group All Red Balls Together
Problems/Minimum Swaps Required to Group All Red Balls Together

Minimum Swaps Required to Group All Red Balls Together

Microsoft logoMicrosoft● MediumFULLTIMEOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

Given a string of red and white balls, return the minimum number of adjacent swaps required to arrange all red balls into one contiguous segment.

Given that S is "RW" repeated 100,000 times, your function should return -1, as the minimum number of swaps required exceeds 10^9.

Function

minSwaps(colors: String) → int

Examples

Example 1

colors = "WRRWR"return = 1

Swap the last red ball with the white ball immediately before it: "WRRWR" becomes "WRRRW". All red balls are contiguous after one adjacent swap.

Example 2

colors = "WWRWWWWWWWWWRWR"return = 10

The red balls start at indices 2, 12, and 14. After subtracting their target offsets 0, 1, and 2, the normalized positions are 2, 11, and 12. Moving them around the median normalized position costs 9 + 0 + 1 = 10 swaps.

Example 3

colors = "WWW"return = 0

There are no red balls that need to be grouped together.

Example 4

colors = "WRRWRW"return = 1

As in the first example, moving the final red ball one position to the left makes all three red balls contiguous.

Constraints

  • N is an integer in the range [1..100,000]
  • The string S consists only of the characters 'R' and 'W'.
  • Constraints updated in full on 05-24-2025 🥑

More Microsoft interview questions

  • Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
  • Authentication SystemOA · Seen Nov 2026
  • Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
  • Maximum String OperationsOA · Seen Nov 2026
  • Minimum Cost to Process Priority TasksOA · Seen Nov 2026
  • Most Visited Sprint MarkerOA · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Earn the Most 🐝Capital One · OACapital One interview questions
  • Reschedule Meetings for Maximum Free TimeFivetran · OAFivetran interview questions
  • Minimum Cost to Convert Products to Variant AAmazon · OA
  • Maximize Element FrequencyGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Minimum Cost to Merge Values into OnePayPal · OAPayPal interview questions
  • Minimum Operations to Sort with Rotations Then SwapsBank of New York Mellon · OA
  • Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
  • Count Subarrays With Product Equal to KSalesforce · OASalesforce interview questions
  • Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
  • Count Servers With No Requests in a Time WindowIBM · OAIBM interview questions

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int minSwaps(String colors) {
      // write your code here
    }

}
Checking account…