Answer: Cisco OA Question 2024 May | Graphs & DFS | on-campus

Answer · Posted Jun 2026

Solution: Number of Islands — DFS Grid Flood-Fill Approach We can solve this problem by treating the grid as an undirected graph and finding the number of **Connected Components** using **Depth First Search (DFS)**. We traverse the grid. When we find a land cell ('1'), we increment our island count and trigger a DFS flood-fill. The DFS visits all adjacent land cells (up, down, left, right) and sinks them (replaces them with '0') to prevent them from being counted again ...

The full answer & interview discussion are available to premium members.

Log in Create a free account