Coding

Find the shortest distance from all buildings to an empty land in a grid.

You are given a grid of values 0, 1, or 2, where 0 is empty land, 1 is a building, and 2 is an obstacle. Find the shortest travel distance from all buildings to an empty land spot.

Input: grid = [[1,0],[0,1]]

Output: 2

Explanation: The empty lands are at (0,1) and (1,0). Both empty lands are exactly 1 step away from each building. Total distance = 1 + 1 = 2.

Was asked at

Practice this question with AI

First session is free - no credit card required.

Go Premium

More interviews, more skills, more success.

Practice More Questions

Interview question asked to Software Engineers interviewing at GoodData, The Descartes Systems Group, Symantec and other companies. Original question asked: Find the shortest distance from all buildings to an empty land in a grid..