01 Matrix
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 neededProblem
You are given an m x n binary matrix mat containing at least one 0. For every cell, compute the distance to the nearest 0, where one step moves to an edge-adjacent cell (up, down, left or right).
Return a matrix of the same shape where each entry is that distance. Cells that already hold 0 get distance 0.
Aim for O(m * n) time.
Examples
Constraints
- 1 <= m, n <= 100
- m * n <= 10^4
- mat[i][j] is 0 or 1
- mat contains at least one 0
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.