Skip to main content
All Questions

Implement countLakes to count the lakes within a given island of a grid.

Unlock detailed company stats for this questionUpgrade

Given a large M x N grid (1 = land, 0 = water/ocean) with multiple disconnected islands surrounded by ocean, and an API countLakes(island, coordinates) where coordinates is a point belonging to the given island, return the number of lakes within that particular island.

A lake is a water region completely enclosed by land. Water connected to the ocean is not counted. An island can have multiple lakes, and there is ocean outside the grid as well.

Example: 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 0 0 1 0 1 0 1 0 0 0 1 1 1 0 1 1 0 0 0 0 0 0 0 0 0 There are 2 disconnected islands. If coordinates belongs to the first island, countLakes returns 1, since it has one enclosed lake.

The problem was initially lengthy and ambiguous; after clarifying questions it boiled down to a DFS-based solution.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.