DSA interview question
A flooded square plaza is an n × n grid of distinct elevations. Rain raises the water level: at time t, water stands at height t everywhere, and a swimmer can move 4-directionally between cells whose elevations are at most t (swimming takes no time). Starting at the top-left cell, what's the earliest time to reach the bottom-right cell? A path is usable at time t exactly when its highest cell is at most t, so the answer is the minimum over paths of the path's maximum elevation. That's Dijkstra with max instead of sum.
Example: [[0,2], [1,3]] → 3.
int swimInWater(const vector<vector<int>>& grid); // n x n, distinct values 0..n²-1
| Input | Output | Explanation |
|---|---|---|
[[0,2],[1,3]] | 3 | The goal itself is at 3 |
[[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]] | 16 | The best path's peak is 16 |
[[3,2],[0,1]] | 3 | The start itself is at 3 |
[[0,1],[2,3]] | 3 | |
[[0]] | 0 |
(Confirm: moves are 4-directional; you can wait anywhere; both the start and goal cells count toward the maximum.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in