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.
Given an n x n binary matrix image, flip the image horizontally, then invert it, and return the resulting image.
To flip an image horizontally means that each row of the image is reversed.
[1,1,0] horizontally results in [0,1,1].To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0.
[0,1,1] results in [1,0,0].Example 1:
Input: image = [[1,1,0],[1,0,1],[0,0,0]] Output: [[1,0,0],[0,1,0],[1,1,1]] Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]]. Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
Example 2:
Input: image = [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]] Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]] Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]]. Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
Constraints:
n == image.lengthn == image[i].length1 <= n <= 20images[i][j] is either 0 or 1.Problem summary: Given an n x n binary matrix image, flip the image horizontally, then invert it, and return the resulting image. To flip an image horizontally means that each row of the image is reversed. For example, flipping [1,1,0] horizontally results in [0,1,1]. To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0,1,1] results in [1,0,0].
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Bit Manipulation
[[1,1,0],[1,0,1],[0,0,0]]
[[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #832: Flipping an Image
class Solution {
public int[][] flipAndInvertImage(int[][] image) {
for (var row : image) {
int i = 0, j = row.length - 1;
for (; i < j; ++i, --j) {
if (row[i] == row[j]) {
row[i] ^= 1;
row[j] ^= 1;
}
}
if (i == j) {
row[i] ^= 1;
}
}
return image;
}
}
// Accepted solution for LeetCode #832: Flipping an Image
func flipAndInvertImage(image [][]int) [][]int {
for _, row := range image {
i, j := 0, len(row)-1
for ; i < j; i, j = i+1, j-1 {
if row[i] == row[j] {
row[i] ^= 1
row[j] ^= 1
}
}
if i == j {
row[i] ^= 1
}
}
return image
}
# Accepted solution for LeetCode #832: Flipping an Image
class Solution:
def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
n = len(image)
for row in image:
i, j = 0, n - 1
while i < j:
if row[i] == row[j]:
row[i] ^= 1
row[j] ^= 1
i, j = i + 1, j - 1
if i == j:
row[i] ^= 1
return image
// Accepted solution for LeetCode #832: Flipping an Image
struct Solution;
impl Solution {
fn flip_and_invert_image(mut a: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let n = a.len();
for i in 0..n {
let mut l = 0;
let mut r = n - 1;
while l < r {
a[i].swap(l, r);
l += 1;
r -= 1;
}
for j in 0..n {
a[i][j] = 1 - a[i][j];
}
}
a
}
}
#[test]
fn test() {
let a: Vec<Vec<i32>> = vec_vec_i32![[1, 1, 0], [1, 0, 1], [0, 0, 0]];
let b: Vec<Vec<i32>> = vec_vec_i32![[1, 0, 0], [0, 1, 0], [1, 1, 1]];
assert_eq!(Solution::flip_and_invert_image(a), b);
let a: Vec<Vec<i32>> = vec_vec_i32![[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]];
let b: Vec<Vec<i32>> = vec_vec_i32![[1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]];
assert_eq!(Solution::flip_and_invert_image(a), b);
}
// Accepted solution for LeetCode #832: Flipping an Image
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #832: Flipping an Image
// class Solution {
// public int[][] flipAndInvertImage(int[][] image) {
// for (var row : image) {
// int i = 0, j = row.length - 1;
// for (; i < j; ++i, --j) {
// if (row[i] == row[j]) {
// row[i] ^= 1;
// row[j] ^= 1;
// }
// }
// if (i == j) {
// row[i] ^= 1;
// }
// }
// return image;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
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.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.