Minimum Swaps to Group Ones in a Square
Updated
Problem statement
You are given a square grid grid where every cell holds a zero or a one. The number of ones is exactly k * k for some positive whole number k. Your goal is to gather all the ones into one solid square block of side k. The block must sit with its sides along the grid rows and columns.
One swap trades the contents of any two cells. The two cells do not have to be next to each other. Return the smallest number of swaps needed. A k x k square always fits inside the grid.
Function
minimumSquareGroupingSwaps(grid: int[][]) → intExamples
Example 1
grid = [[1,0,0],[1,0,1],[0,1,0]]return = 2The grid has four ones, so the target is a 2 by 2 square. The best choice, such as the top-left square, already holds two ones, and no square holds more. Each swap moves one outside one in, so the answer is two.
Example 2
grid = [[1,1,0],[1,1,0],[0,0,0]]return = 0The four ones already fill the top-left square of side two. No swaps are needed, so the answer is zero.
Example 3
grid = [[1,0,1],[0,0,0],[1,0,1]]return = 3The four ones sit in the four corners. Every 2 by 2 square holds only one of them, so three ones must be swapped in. The answer is three.
Constraints
1 <= grid.length <= 100.- Every row has as many cells as there are rows, so the grid is square.
- Every cell is
0or1. - The number of ones is a positive perfect square
k * k, with1 <= k <= grid.length.
More Amazon Phone Screen questions
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Second Largest Distinct ElementPHONE SCREEN · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Calendar Availability for at Least P PeopleGoogle · ONSITE INTERVIEWGoogle interview questions
- Maximum-Scoring Consecutive Tree LevelsJane Street · PHONE SCREENJane Street interview questions
- File-Backed Dense Game of LifeApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Maximum Value for Two Delivery RobotsNuro · PHONE SCREENNuro interview questions
- Longest Run of Ones After Flipping k ZerosC3.ai · PHONE SCREENC3.ai interview questions
- Matrix Region and Ring SumsNVIDIA · PHONE SCREENNVIDIA interview questions
- Count Non-Overlapping Pairs of Rectangles That Sum to KWisdomAI · PHONE SCREEN
- Prefix Matrix Products and AutogradOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Split a Matrix Into Four Partitions by Average (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Shortest Path Around Rectangular ObstaclesDRW · OADRW interview questions
Amazon interview experiences
- Amazon Experienced SDE Onsite Questions: 1 Experience
- Amazon SDE Onsite Interview Questions: 9 Experiences
- Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
- Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
- Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
- Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)