Fewest Swaps to Cluster All Sponsored Posts in a Circular Carousel

Last updated: November 29, 2026

Quick Overview

A circular carousel holds organic posts (0) and sponsored posts (1). Return the minimum number of swaps of any two positions needed to make all sponsored posts contiguous, allowing the group to wrap around the end.

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

0

0

0 solved


A circular carousel holds organic posts (0) and sponsored posts (1). Return the minimum number of swaps of any two positions needed to make all sponsored posts contiguous, allowing the group to wrap around the end.

A fixed-size sliding window on a circular array, framed around Instagram carousel layouts. A Meta interviewer looks for the insight that the window size equals the number of sponsored posts and that the circle is handled by indexing modulo n.

What the Interviewer Expects
  • Realizes the final block has length equal to the number of 1s
  • Shows that the swaps needed for a window equal the number of 0s inside it
  • Slides a window of that length around the circle using modulo indexing
  • Avoids actually doubling the array, or explains the trade-off if it does
  • Gives O(n) time and O(1) space
Key Topics to Cover
sliding window
circular array
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
  • The carousel is linear, not circular. What changes?
  • Swaps can only be between adjacent positions. How does the answer change?
  • Group three post types at once.
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: posts, a list of 0s and 1s. Output: the minimum number of swaps so the 1s form one contiguous block on the circle. Any two positions may be swapped....

High-Level Design

Let m be the number of 1s. Whatever final block we pick has length m, and each 0 inside that block must be swapped with a 1 outside it, one swap each. So the answer is m minus the maximum number...


Submit Your Answer
Markdown supported