0
0
DSA Typescriptprogramming~20 mins

Number of Islands BFS and DFS in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Island Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of BFS traversal on grid
What is the number of islands found by BFS in the given grid?
DSA Typescript
const grid = [
  ['1','1','0','0','0'],
  ['1','1','0','0','0'],
  ['0','0','1','0','0'],
  ['0','0','0','1','1']
];

function numIslandsBFS(grid: string[][]): number {
  if (!grid.length) return 0;
  const rows = grid.length;
  const cols = grid[0].length;
  let count = 0;
  const directions = [[1,0],[-1,0],[0,1],[0,-1]];

  for (let r = 0; r < rows; r++) {
    for (let c = 0; c < cols; c++) {
      if (grid[r][c] === '1') {
        count++;
        const queue = [[r,c]];
        grid[r][c] = '0';
        while(queue.length) {
          const [x,y] = queue.shift()!;
          for (const [dx,dy] of directions) {
            const nx = x + dx;
            const ny = y + dy;
            if (nx >= 0 && nx < rows && ny >= 0 && ny < cols && grid[nx][ny] === '1') {
              queue.push([nx, ny]);
              grid[nx][ny] = '0';
            }
          }
        }
      }
    }
  }
  return count;
}

console.log(numIslandsBFS(grid));
A1
B2
C4
D3
Attempts:
2 left
💡 Hint
Count connected groups of '1's using BFS.
Predict Output
intermediate
2:00remaining
Output of DFS traversal on grid
What is the number of islands found by DFS in the given grid?
DSA Typescript
const grid = [
  ['1','1','0','0','0'],
  ['1','1','0','0','0'],
  ['0','0','1','0','0'],
  ['0','0','0','1','1']
];

function numIslandsDFS(grid: string[][]): number {
  if (!grid.length) return 0;
  const rows = grid.length;
  const cols = grid[0].length;
  let count = 0;

  function dfs(r: number, c: number) {
    if (r < 0 || c < 0 || r >= rows || c >= cols || grid[r][c] === '0') return;
    grid[r][c] = '0';
    dfs(r+1, c);
    dfs(r-1, c);
    dfs(r, c+1);
    dfs(r, c-1);
  }

  for (let r = 0; r < rows; r++) {
    for (let c = 0; c < cols; c++) {
      if (grid[r][c] === '1') {
        count++;
        dfs(r, c);
      }
    }
  }
  return count;
}

console.log(numIslandsDFS(grid));
A1
B3
C2
D4
Attempts:
2 left
💡 Hint
Use DFS to mark all connected land cells.
🧠 Conceptual
advanced
1:30remaining
Why mark visited cells in Number of Islands?
Why do BFS and DFS algorithms mark visited land cells as '0' or visited when counting islands?
ATo avoid counting the same island multiple times by revisiting cells.
BTo increase the number of islands found by splitting them.
CTo convert water cells into land cells for easier traversal.
DTo speed up the algorithm by skipping water cells.
Attempts:
2 left
💡 Hint
Think about what happens if you revisit the same land cell.
Predict Output
advanced
2:00remaining
Output after modifying grid during DFS
What is the final state of the grid after running DFS-based number of islands on this input?
DSA Typescript
const grid = [
  ['1','0','1'],
  ['0','1','0'],
  ['1','0','1']
];

function numIslandsDFS(grid: string[][]): number {
  if (!grid.length) return 0;
  const rows = grid.length;
  const cols = grid[0].length;
  let count = 0;

  function dfs(r: number, c: number) {
    if (r < 0 || c < 0 || r >= rows || c >= cols || grid[r][c] === '0') return;
    grid[r][c] = '0';
    dfs(r+1, c);
    dfs(r-1, c);
    dfs(r, c+1);
    dfs(r, c-1);
  }

  for (let r = 0; r < rows; r++) {
    for (let c = 0; c < cols; c++) {
      if (grid[r][c] === '1') {
        count++;
        dfs(r, c);
      }
    }
  }
  return count;
}

numIslandsDFS(grid);
console.log(grid);
A[['0','0','1'],['0','0','0'],['1','0','0']]
B[['1','0','1'],['0','1','0'],['1','0','1']]
C[['0','0','0'],['0','0','0'],['0','0','0']]
D[['0','0','0'],['0','1','0'],['0','0','0']]
Attempts:
2 left
💡 Hint
DFS changes all connected '1's to '0's.
🧠 Conceptual
expert
1:30remaining
Time complexity of Number of Islands algorithms
What is the time complexity of BFS or DFS approach to count the number of islands in an m x n grid?
AO(m * n), because each cell is visited at most once.
BO(m + n), because BFS/DFS only traverse rows and columns separately.
CO((m * n)^2), because each cell is compared with every other cell.
DO(1), because the grid size is fixed.
Attempts:
2 left
💡 Hint
Consider how many times each cell is processed.