Question bank › Graphs Bfs
Dsa Graphs Bfs Hard

Maximum Distance in a Grid

You are given a 2D grid of 0s and 1s where 0 represents water and 1 represents land. Your task is to find the maximum distance from a water cell to the nearest land cell. The distance is defined as the minimum number of steps required to reach from a water cell to a land cell. You can move up, down, left, or right from a cell. If no water cell exists, print -1. Input Format: - The first line contains two integers R (rows) and C (columns). - The next R lines contain C integers each, where each integer is either 0 (water) or 1 (land). Output Format: - Print the maximum distance of any water cell to the nearest land cell, or -1 if there are no water cells. Example: Input: 3 3 0 0 0 0 1 0 0 0 0 Output: 2

Key concepts

graphsbfsshortest path

Practise this out loud — free

Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.

🎙 Practise this question now
Part of Praxari's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.