DSA interview question
A satellite image is a grid where '1' is land and '0' is water. For a coastline survey, two land cells belong to the same island if they touch horizontally, vertically or diagonally (8 directions). Count the islands. Each island is a connected component of land cells. Scan the grid, and every time you find land that hasn't been visited, count a new island and flood-fill it with BFS or DFS over all 8 neighbours, marking it visited.
Example: grid 0111000 / 0011010 → 2.
int numIslands8(const vector<string>& grid); // '1' land, '0' water; 8-directional
| Input (rows) | Output | Explanation |
|---|---|---|
01 / 10 / 11 / 10 | 1 | The diagonal joins the top 1 to the rest |
0111000 / 0011010 | 2 | The single 1 at the far right is separate |
101 / 010 / 101 | 1 | All joined through the centre diagonally |
100 / 001 | 2 | Not adjacent, even diagonally |
000 | 0 |
(Confirm: adjacency includes the four diagonals; the grid may be non-square.)
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