Magicsheet logo

Delete Greatest Value in Each Row

Easy
12.5%
Updated 8/1/2025

Delete Greatest Value in Each Row

What is this problem about?

The Delete Greatest Value in Each Row interview question is a grid-based simulation. In each step, you must find and delete the largest element from every row. Among the values deleted in that step, identify the maximum one and add it to your total score. Repeat this until the grid is empty. This Delete Greatest Value in Each Row coding problem is about repeated maximum identification.

Why is this asked in interviews?

Amazon and Google use this "Easy" question to check for basic algorithmic optimization. While you can search for the max in each row repeatedly (O(N^2 * M)), sorting each row first (O(N * M log M)) makes the process much more efficient. It tests if a candidate can recognize that pre-processing (sorting) simplifies a multi-step simulation.

Algorithmic pattern used

This follows the Array, Matrix, Sorting, Simulation interview pattern.

  1. Pre-process: Sort every row of the matrix in ascending order.
  2. Iterate: Iterate through the columns from last to first (or first to last, since rows are sorted).
  3. Identify Step Max: For each column index j, find the maximum value across all rows at that index.
  4. Accumulate: Add these step-maxima to your answer.

Example explanation

Grid:

[1, 2, 4]
[3, 3, 1]
  1. Sort rows: [1, 2, 4] [1, 3, 3]
  2. Step 1 (Col 2): max(4, 3) = 4. Score = 4.
  3. Step 2 (Col 1): max(2, 3) = 3. Score = 4 + 3 = 7.
  4. Step 3 (Col 0): max(1, 1) = 1. Score = 7 + 1 = 8. Final Result: 8.

Common mistakes candidates make

  • Repeated Max Search: Searching for the max in each row manually in every iteration, which is slow.
  • Incorrect Step Max: Adding the sum of deleted values instead of the maximum of the deleted values for each step.
  • Handling different row lengths: Usually, the grid is rectangular, but always check if rows can have varying lengths.

Interview preparation tip

Whenever a problem asks you to repeatedly perform an operation on the "greatest" or "smallest" element, ask yourself if sorting the data once at the beginning can replace the repeated search. Sorting is often the key to moving from O(N^2) to O(N log N).

Similar Questions