← Bloomberg Interview Insights
It's Number of Islands but you can't just count them, you need to track the size of each one.
Use BFS or DFS to traverse each unvisited land cell, marking visited cells and counting the size of each island. Store island sizes in a frequency map, then for each target size, look up its count in O(1) time. This yields O(m*n + t) time and O(m*n) space.
Pro tip: Mention that you can avoid modifying the input grid by using a separate visited matrix or by temporarily mutating the grid and restoring it, and discuss trade-offs. Also, note that if the grid is very large, a union-find approach can be more cache-friendly and parallelizable.
Confirm the definition of 4-connectivity, input constraints (e.g., grid size, target array size), and expected output format. Decide on traversal method (BFS/DFS) and data structures.
Iterate through each cell; when encountering unvisited land, perform BFS/DFS to explore the entire island, marking cells as visited and counting its size.
Store each island's size in a hash map (size -> frequency) or a list if sizes are bounded. This allows O(1) lookup for target sizes.
For each target size, retrieve its frequency from the map (or 0 if absent) and append to the result array.
State time complexity O(m*n + t) and space complexity O(m*n) for visited tracking, and discuss potential optimizations like union-find or early termination.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.