Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given a 2D matrix grid of size m x n. You need to check if each cell grid[i][j] is:
grid[i][j] == grid[i + 1][j] (if it exists).grid[i][j] != grid[i][j + 1] (if it exists).Return true if all the cells satisfy these conditions, otherwise, return false.
Example 1:
Input: grid = [[1,0,2],[1,0,2]]
Output: true
Explanation:
All the cells in the grid satisfy the conditions.
Example 2:
Input: grid = [[1,1,1],[0,0,0]]
Output: false
Explanation:
All cells in the first row are equal.
Example 3:
Input: grid = [[1],[2],[3]]
Output: false
Explanation:
Cells in the first column have different values.
Constraints:
1 <= n, m <= 100 <= grid[i][j] <= 9Problem summary: You are given a 2D matrix grid of size m x n. You need to check if each cell grid[i][j] is: Equal to the cell below it, i.e. grid[i][j] == grid[i + 1][j] (if it exists). Different from the cell to its right, i.e. grid[i][j] != grid[i][j + 1] (if it exists). Return true if all the cells satisfy these conditions, otherwise, return false.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[1,0,2],[1,0,2]]
[[1,1,1],[0,0,0]]
[[1],[2],[3]]
candy)distribute-candies)minimum-cost-of-buying-candies-with-discount)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3142: Check if Grid Satisfies Conditions
class Solution {
public boolean satisfiesConditions(int[][] grid) {
int m = grid.length, n = grid[0].length;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (i + 1 < m && grid[i][j] != grid[i + 1][j]) {
return false;
}
if (j + 1 < n && grid[i][j] == grid[i][j + 1]) {
return false;
}
}
}
return true;
}
}
// Accepted solution for LeetCode #3142: Check if Grid Satisfies Conditions
func satisfiesConditions(grid [][]int) bool {
m, n := len(grid), len(grid[0])
for i, row := range grid {
for j, x := range row {
if i+1 < m && x != grid[i+1][j] {
return false
}
if j+1 < n && x == grid[i][j+1] {
return false
}
}
}
return true
}
# Accepted solution for LeetCode #3142: Check if Grid Satisfies Conditions
class Solution:
def satisfiesConditions(self, grid: List[List[int]]) -> bool:
m, n = len(grid), len(grid[0])
for i, row in enumerate(grid):
for j, x in enumerate(row):
if i + 1 < m and x != grid[i + 1][j]:
return False
if j + 1 < n and x == grid[i][j + 1]:
return False
return True
// Accepted solution for LeetCode #3142: Check if Grid Satisfies Conditions
/**
* [3142] Check if Grid Satisfies Conditions
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn satisfies_conditions(grid: Vec<Vec<i32>>) -> bool {
let (m, n) = (grid.len(), grid[0].len());
for i in 0..n {
if i != 0 && grid[0][i] == grid[0][i - 1] {
return false;
}
for j in 1..m {
if grid[j][i] != grid[j - 1][i] {
return false;
}
}
}
true
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3142() {
assert!(Solution::satisfies_conditions(vec![
vec![1, 0, 2],
vec![1, 0, 2]
]));
assert!(!Solution::satisfies_conditions(vec![
vec![1, 1, 1],
vec![0, 0, 0]
]));
assert!(Solution::satisfies_conditions(vec![vec![0]]));
assert!(!Solution::satisfies_conditions(vec![
vec![1],
vec![2],
vec![3]
]));
}
}
// Accepted solution for LeetCode #3142: Check if Grid Satisfies Conditions
function satisfiesConditions(grid: number[][]): boolean {
const [m, n] = [grid.length, grid[0].length];
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (i + 1 < m && grid[i][j] !== grid[i + 1][j]) {
return false;
}
if (j + 1 < n && grid[i][j] === grid[i][j + 1]) {
return false;
}
}
}
return true;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.