Minimum Swaps to Cluster Prime Bins on a Circular Sorting Carousel

Last updated: November 29, 2026

Quick Overview

A circular carousel has slots marked 1 (bin holding Prime-eligible items) or 0. Return the minimum number of swaps of any two slots needed to make all the 1s contiguous somewhere around the circle.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


A circular carousel has slots marked 1 (bin holding Prime-eligible items) or 0. Return the minimum number of swaps of any two slots needed to make all the 1s contiguous somewhere around the circle.

An Amazon onsite question set on a fulfillment center sortation carousel. The interviewer expects you to translate swaps into a fixed-size window count, then handle the wraparound without building a messy special case.

What the Interviewer Expects
  • Observe the final cluster has length equal to the number of 1s
  • Count zeros inside each window of that length
  • Explain why each zero in the window costs exactly one swap
  • Handle wraparound with modular indexing instead of copying
  • Cover the all-zeros and all-ones cases
Key Topics to Cover
Fixed Sliding Window
Circular Arrays
Counting
Greedy
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 swaps are only allowed between adjacent slots?
  • Return the starting slot of the best cluster as well.
  • What if the carousel is linear rather than circular?
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 slots of 0s and 1s, length n up to 1e5, arranged in a circle. Output: minimum swaps (any two positions) so all 1s are consecutive on the circle....

High-Level Design

Let k be the number of 1s. Any final arrangement places all 1s in some window of length k. For a chosen window, every 0 inside must be swapped with a 1 outside, and one swap fixes one 0, so the cost...


Submit Your Answer
Markdown supported