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.
Input: ([[0, 2, 0, 4], [3, 5, 0, -1], [0, 0, 7, 8]],)
Expected Output: 18
Explanation: The island containing 4, -1, 7, and 8 has sum 18, which is maximum.
Input: ([[-5, 0, -2], [0, -3, 0]],)
Expected Output: -2
Explanation: All islands are single negative cells; the maximum sum is -2.
Input: ([[1, -2, 3], [0, 0, 4]],)
Expected Output: 6
Explanation: All non-zero cells are connected into one island with sum 1 + (-2) + 3 + 4 = 6.
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.
Input: ([[2, -1, 3], [0, 4, 0], [5, 5, 0]],)
Expected Output: 0
Explanation: The island containing 2, -1, 3, and 4 is ignored because it has a negative cell. The bottom island has sum 10.
Input: ([[-1, 2], [0, 0]],)
Expected Output: 0
Explanation: The only island contains a negative cell, so it is ignored.
Input: ([[0, -2, 0], [1, 0, 2], [1, 0, -3]],)
Expected Output: 2
Explanation: The left island with cells 1 and 1 is valid and has sum 2. Islands containing -2 or -3 are invalid.
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.
Input: ([[1, 1, 0, 2], [0, 0, 0, 2], [3, 0, 4, 4]],)
Expected Output: (12, (0, 3))
Explanation: The maximum island contains 2, 2, 4, and 4 for sum 12. The first encountered cell in that island is (0, 3).
Input: ([[-5, 0], [-2, 0]],)
Expected Output: (-7, (0, 0))
Explanation: There are two negative singleton islands. The larger sum is -2 at coordinate (1, 0).
Input: ([[1, 0, 1]],)
Expected Output: (1, (0, 0))
Explanation: Both singleton islands tie with sum 1. The first one in row-major scan is chosen.
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).
Input: ([[5, 0, 5], [0, 0, 0], [2, 3, 0]],)
Expected Output: (5, (2, 1))
Explanation: Three islands tie with sum 5. Their last indices are (0, 0), (0, 2), and (2, 1), so (2, 1) wins.
Input: ([[4, -1, 0], [0, 0, 2], [3, 0, 2]],)
Expected Output: (4, (2, 2))
Explanation: The island containing the two 2s has maximum sum 4 and last index (2, 2).
Input: ([[-1, 0, -1], [0, -2, 0]],)
Expected Output: (-1, (0, 2))
Explanation: Two islands tie for maximum sum -1. The later last index is (0, 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.