Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
What are the constraints?
m
== grid.length (number of rows)n
== grid[i].length (number of columns)m
, n
<= 300grid[i][j]
is '0' or '1'Can we simply iterate through the grid and count each time that there isn’t a 1 to the left or above another 1?
What is the definition of an island?
HAPPY CASE
Input: [["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]
Output: 1
Input: [["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]
Output: 3
EDGE CASE
Input: [["0","0","1","0","0"],["0","0","0","0","0"],["0","0","0","0","0"],["0","0","1","0","0"]]
Output: 2
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For this graph problem, some things we want to consider are:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: iteratively search the neighbors of enqueued nodes until the queue becomes empty.
1. Take visited[m][m] boolean array and initialize all values to false
- Set parent[i] = i for all elements
2. For each element of the grid
- Check boundary conditions. If this element is '1'
a. If the left neighbor is '1', call union
b. If the upper neighbor is '1', call union
3. Return the number of islands (sets)
⚠️ Common Mistakes
Implement the code to solve the algorithm.
public int numIslands(char[][] grid) {
// return 0 if grid is empty or out of bounds
if(grid == null || grid.length < 1 || grid[0].length < 1){
return 0;
}
// initialize variables
int row = grid.length;
int col = grid[0].length;
int[] root = new int[row * col];
Arrays.fill(root, -1);
int n = 0;
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
root[i*col + j] = i*col + j;
if(grid[i][j] == '1'){
n++;
}
}
}
// Check boundary conditions
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
if(grid[i][j] == '1'){
int p = i * col + j;
int q;
if((i+1) < row && grid[i+1][j] == '1'){
q = p + col;
n = union(root, p, q, n);
}
if((j+1) < col && grid[i][j+1] == '1'){
q = p + 1;
n = union(root, p, q, n);
}
}
}
}
// return the number of islands
return n;
}
private int union(int[] root, int p, int q, int count){
int proot = find(root, p);
int qroot = find(root, q);
if(proot!= qroot){
root[proot] = qroot;
count--;
}
return count;
}
private int find(int[] root, int p){
while(p != root[p]){
root[p] = root[root[p]];
p = root[p];
}
return p;
}
def numIslands(self, grid: List[List[str]]) -> int:
# keep track of row and column lengths of grid to access later
m = len(grid)
n = len(grid[0])
def destroyIsland(r,c): # recursive DFS helper
grid[r][c] = "2" # set this spot to a different value to avoid infinite loops
for (row,col) in [(r-1,c),(r+1,c),(r,c-1),(r,c+1)]: # 4-dimensionally adjacent locations
if 0 <= row < m and 0 <= col < n and grid[row][col] == "1": # if not out of bounds and part of the island
destroyIsland(row,col) # destroy the rest of the island (set to "2")
ans = 0 # answer
for i, row in enumerate(grid): # get rows from grid
for j, element in enumerate(row): # go through the row
if element == "1": # if we hid land
destroyIsland(i,j) # destroy this island
ans += 1 # we found an island
return ans
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Calculate complexity in terms of:
- the number of grid elements, `V`
- the size of the maximum island, `I`
Time complexity:
- `O(V)` to build array
- `O(V)` iterations
- `O(log I)` for union operation
- Total: **O(V log I)**
- Can ensure `O(V log V)` by tuning union function
Space complexity: O(V)