Find maximum island sum and required indices
Company: DocuSign
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates grid-based graph traversal and connected-component aggregation skills, including handling positive and negative cell values, tie-breaking by row-major indices, and computing maximum component sums.
Part 1: Maximum Island Sum
Constraints
- 0 <= m, n <= 500
- 0 <= m * n <= 200000
- -10^6 <= grid[r][c] <= 10^6
- grid is rectangular when non-empty
- 0 represents water; any non-zero value represents land
Examples
Input: ([],)
Expected Output: 0
Explanation: The grid is empty, so there are no islands.
Input: ([[0, 0], [0, 0]],)
Expected Output: 0
Explanation: All cells are water.
Hints
- Treat every unvisited non-zero cell as the start of a new connected component.
- Use DFS or BFS to visit the whole island while accumulating its sum.
Part 2: Maximum Valid Island Sum Ignoring Islands With Negative Cells
Constraints
- 0 <= m, n <= 500
- 0 <= m * n <= 200000
- -10^6 <= grid[r][c] <= 10^6
- grid is rectangular when non-empty
- 0 represents water; any non-zero value represents land
- A valid island must contain no negative land cells
Examples
Input: ([],)
Expected Output: 0
Explanation: The grid is empty, so there are no valid islands.
Input: ([[1, 2, 0], [0, 3, 0], [4, 0, 5]],)
Expected Output: 6
Explanation: The island containing 1, 2, and 3 has sum 6, greater than the singleton islands 4 and 5.
Hints
- You still need to traverse an invalid island completely so its cells are marked visited.
- While exploring an island, track both its sum and whether any cell is negative.
Part 3: Maximum Island Sum With Any Cell Index
Constraints
- 0 <= m, n <= 500
- 0 <= m * n <= 200000
- -10^6 <= grid[r][c] <= 10^6
- grid is rectangular when non-empty
- 0 represents water; any non-zero value represents land
- Returned coordinates must be 0-based
Examples
Input: ([],)
Expected Output: (0, (-1, -1))
Explanation: There are no islands.
Input: ([[0, 5, 0], [0, 5, 0]],)
Expected Output: (10, (0, 1))
Explanation: The vertical island has sum 10, and (0, 1) is a cell in it.
Hints
- When starting DFS or BFS for an island, save the starting coordinate as a valid representative cell.
- Update the best answer after finishing a whole island, not while you are still exploring it.
Part 4: Maximum Island Sum With Row-Major Last Index Tie-Breaker
Constraints
- 0 <= m, n <= 500
- 0 <= m * n <= 200000
- -10^6 <= grid[r][c] <= 10^6
- grid is rectangular when non-empty
- 0 represents water; any non-zero value represents land
- The last index of an island is the maximum coordinate by row-major order
Examples
Input: ([],)
Expected Output: (0, (-1, -1))
Explanation: There are no islands.
Input: ([[1, 2, 0], [0, 3, 4]],)
Expected Output: (10, (1, 2))
Explanation: All land cells are connected with sum 10. The last index in that island is (1, 2).
Hints
- During DFS or BFS of an island, keep updating the largest coordinate seen in that island.
- When comparing islands, compare sums first; only if sums are equal compare their last indices.