Flipping an Image

EasyMatrixtwo pointers

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an n x n binary matrix image. Transform it in two steps and return the result. First, **flip** each row horizontally, so every row is reversed. Then **invert** every cell, turning 0 into 1 and 1 into 0. You may modify image in place and return it, or return a new matrix.

Examples

Input: image = [[1,1,0],[1,0,1],[0,0,0]]
Output: [[1,0,0],[0,1,0],[1,1,1]]
Reversing gives [[0,1,1],[1,0,1],[0,0,0]]; inverting gives the output.
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]]
Each row is reversed and then every bit is toggled.

Constraints

  • n == image.length == image[i].length
  • 1 <= n <= 20
  • `image[i][j]` is `0` or `1`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More two pointers problems