← Bytedance Interview Insights
The basic island count with BFS/DFS I can do in my sleep, but the 'distinct' part tripped me up for a bit.
Use DFS/BFS to find each island, then normalize its shape by translating it to a canonical form (e.g., relative coordinates from the top-left cell). Store these normalized shapes in a set to count distinct islands.
Pro tip: Emphasize that translation invariance is achieved by using relative coordinates, and mention that the order of traversal doesn't matter as long as the normalization is consistent.
Iterate through each cell; when encountering unvisited land (1), start a DFS/BFS to explore the entire island.
During traversal, record the absolute coordinates of all cells belonging to the island.
Translate the island to a canonical form by subtracting the minimum row and column from all coordinates, making it invariant to translation.
Insert the normalized shape (e.g., as a sorted list of coordinates or a string) into a set; the set size gives the number of distinct islands.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.