Fewest Swaps to Group Healthy Servers Together in a Circular Rack

Last updated: November 29, 2026

Quick Overview

A circular rack is a list of 1 (healthy) and 0 (drained) slots where the last slot is adjacent to the first. Return the minimum number of swaps of any two slots needed to make all healthy servers contiguous around the ring.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


A circular rack is a list of 1 (healthy) and 0 (drained) slots where the last slot is adjacent to the first. Return the minimum number of swaps of any two slots needed to make all healthy servers contiguous around the ring.

A Google technical-screen window problem framed around datacenter rack maintenance. The interviewer checks that the candidate fixes the window size at the number of healthy servers, counts zeros inside it, and handles wraparound without copying the array twice.

What the Interviewer Expects
  • Sets the window size to the total number of ones
  • Realizes swaps needed equals zeros inside the best window
  • Handles the circular case with modular indexing
  • Handles zero or all healthy servers
  • States O(n) time and O(1) space
Key Topics to Cover
Fixed Window
Circular Arrays
Sliding Window
Counting
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if the rack is linear rather than circular?
  • What if only adjacent swaps are allowed?
  • How would you handle three server states that must each be grouped?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: list rack of 0s and 1s, treated as circular.
  • One swap exchanges any two slots.
  • Output: minimum swaps so all 1s form one contiguous block on the ring....
High-Level Design

Let ones be the number of healthy servers. In the final arrangement they occupy some window of exactly ones consecutive slots on the ring. For a candidate window, each 0 inside must be swapped wit...


Submit Your Answer
Markdown supported