← Samsung Interview Insights

Samsung·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Samsung coding interview, classic grid problem. Nothing too surprising but the follow-up on space complexity tripped me up a bit.

Questions Asked (1)

Q1

Given a binary grid where 1s represent land and 0s represent water, how would you count the number of distinct islands?

Algorithms & Data Structures
Author's notes

Went with DFS pretty much immediately, which felt right.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Treat the grid as a graph where each land cell is a node connected to its adjacent land cells. Use DFS or BFS to explore and mark each connected component, incrementing a counter for each new component found. This efficiently counts distinct islands in O(m*n) time.

Pro tip: Mention that you can optimize space by modifying the grid in-place to mark visited cells (e.g., changing '1' to '0'), but clarify that if the input must be preserved, a separate visited matrix or union-find is needed. Also, discuss handling edge cases like empty grid or all water.

1. Clarify the problem

Confirm that islands are formed by horizontally or vertically adjacent land cells, and that diagonal connections do not count. Ask about grid size constraints and whether the grid can be modified.

2. Choose an algorithm

Select either DFS, BFS, or Union-Find. DFS/BFS are simpler and efficient for this problem; Union-Find is an alternative if you prefer iterative approaches or need to handle dynamic updates.

3. Implement traversal

Iterate through each cell. When a land cell is found, increment the island count and perform DFS/BFS to mark all connected land cells as visited (e.g., set to '0' or use a visited set).

4. Analyze complexity

State that time complexity is O(m*n) since each cell is visited once, and space complexity is O(m*n) in the worst case for the recursion stack or queue, but can be optimized to O(min(m,n)) with BFS if using a queue.

5. Test with edge cases

Mention testing with an empty grid, a grid with no land, a single island, multiple disconnected islands, and a grid where all cells are land (one island).

Key Points to Mention

  • Graph traversal techniques: DFS (recursive/iterative) and BFS
  • In-place modification to mark visited cells (changing '1' to '0')
  • Time complexity O(m*n) and space complexity O(m*n) worst-case
  • Handling edge cases: empty grid, all water, all land
  • Alternative approach: Union-Find (Disjoint Set Union) for counting connected components
  • Avoiding stack overflow with iterative DFS or BFS for large grids

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.