PPractick

DSA interview question

Swim in Rising Water

Medium · Data structures · Topics: graph, dijkstra, binary search, matrix

The problem

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.

Function signature

int swimInWater(const vector<vector<int>>& grid);  // n x n, distinct values 0..n²-1

Test cases

InputOutputExplanation
[[0,2],[1,3]]3The 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]]16The best path's peak is 16
[[3,2],[0,1]]3The 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.)

How interviewers phrase it

Clarifying questions to ask

  1. Does the start cell's elevation count? (Yes: you can't be in the water there until t reaches it.)
  2. 4-directional? (Yes.)
  3. How large? (n ≤ 50, and values are a permutation of 0..n² − 1.)

Follow-ups to expect

  1. "Why is this a minimax path problem?"
  2. "Why does Dijkstra work with max?"
  3. "Binary search alternative?"
  4. "Union-find alternative?"
  5. "Complexity?"

Practise questions like this with a real engineer

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 interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in