Fewest Uniform Steps to Bring Every Server Grid Cell Equal
Last updated: November 29, 2026
Quick Overview
Given an m x n grid of integer load values and a step size x, one operation adds or subtracts x from a single cell; return the minimum number of operations to make all cells equal, or -1 if it is impossible.
0
0
0 solved
Given an m x n grid of integer load values and a step size x, one operation adds or subtracts x from a single cell; return the minimum number of operations to make all cells equal, or -1 if it is impossible.
A Google screen problem that is really about two observations: feasibility depends on remainders mod x, and the optimal common value is a median. The interviewer checks that you justify the median rather than trying every target.
What the Interviewer Expects
- Checks that every value has the same remainder modulo x
- Flattens the grid and divides out x
- Chooses the median as the target and explains why
- Sums absolute differences divided by x
- States O(mn log(mn)) time or O(mn) with quickselect
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Prove the median minimizes the sum of absolute deviations.
- Operations on a cell cost different amounts per cell; what target is optimal now?
- Values stream in; maintain the answer after each new cell.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
grid(m x n ints) andx >= 1. - Each operation changes one cell by +x or -x.
- Output: minimum operations to make all equal, or -1....
High-Level Design
A cell value v can only reach values congruent to v mod x, so all cells must share one remainder, otherwise return -1. If they do, the cost to bring every value to target t is sum(|v - t|) / x...