Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: A grid has '.' for open cells and '#' for walls: rows are ".#..#", ".#.##", "...#.", "##..#" and "#...." (5×5). Cells connect only up, down, left and right. How many connected groups of open cells are there?

  1. A. 3
  2. B. 1
  3. C. 2
  4. D. 4

Correct answer: C. 2

A flood fill from each unvisited open cell counts one group per fill. Counting open cells or walls instead of groups is the common mistake.

More Coding Interview Problems questions

Exams that ask this topic