DSA interview question
A land-reclamation project can fill at most one water cell (turn a 0 into 1) on an n × n map. What's the largest island (4-directionally connected land) that can result? Trying every flip and re-running a flood fill is O(n⁴). Instead:
Example: [[1,0], [0,1]] → 3.
int largestIsland(vector<vector<int>> grid); // n x n of 0/1
| Input | Output | Explanation |
|---|---|---|
[[1,0],[0,1]] | 3 | Filling either 0 joins both islands |
[[1,1],[1,0]] | 4 | |
[[1,1],[1,1]] | 4 | Nothing to fill |
[[1,0,1],[0,0,0],[1,0,1]] | 3 | An edge-centre cell joins two corners |
[[0,0],[0,0]] | 1 | Only the filled cell |
(Confirm: at most one cell is changed; connectivity is 4-directional.)
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