Bird
Raised Fist0
Interview Prepsort-and-searchmediumAmazonGoogle

Sort Matrix Diagonally

Choose your preparation mode4 modes available
▶
Steps
setup

Initialize matrix dimensions and diagonal map

We determine the number of rows (m) and columns (n) in the matrix and create an empty map to hold min-heaps for each diagonal key (row - column).

💡 Knowing matrix size and preparing the data structure for diagonals is essential before processing elements.
Line:m, n = len(mat), len(mat[0]) diagonals = collections.defaultdict(list)
💡 The diagonal key is defined as row index minus column index, which uniquely identifies each diagonal.
📊
Sort Matrix Diagonally - Watch the Algorithm Execute, Step by Step
Watching each step of the extraction, sorting, and reinsertion makes it clear how the algorithm handles diagonals independently and why this approach is efficient.
Step 1/25
·Active fill★Answer cell
setup
3
0
3
1
1
2
1
3
2
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
move_right
j
3
0
3
1
1
2
1
3
2
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: {"0":[3]}
move_right
i
3
0
j
3
1
1
2
1
3
2
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: {"0":[3],"-1":[3]}
move_right
i
3
0
3
1
j
1
2
1
3
2
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: {"0":[3],"-1":[3],"-2":[1]}
move_down
i
3
0
3
1
1
2
j
1
3
2
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: {"0":[3],"-1":[3],"-2":[1],"-3":[1]}
move_right
j
3
0
i
3
1
1
2
1
3
2
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: {"0":[3],"1":[2],"-1":[3],"-2":[1],"-3":[1]}
move_right
3
0
j
3
1
1
2
1
3
2
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: {"0":[3,2],"1":[2],"-1":[3],"-2":[1],"-3":[1]}
move_right
3
0
i
3
1
j
1
2
1
3
2
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: {"0":[3,2],"1":[2],"-1":[3,1],"-2":[1],"-3":[1]}
move_down
3
0
i
3
1
1
2
j
1
3
2
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: {"0":[3,2],"1":[2],"-1":[3,1],"-2":[1,2],"-3":[1]}
move_right
j
3
0
3
1
i
1
2
1
3
2
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: {"0":[3,2],"1":[2],"2":[1],"-1":[3,1],"-2":[1,2],"-3":[1]}
move_right
3
0
j
3
1
i
1
2
1
3
2
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: {"0":[3,2],"1":[2,1],"2":[1],"-1":[3,1],"-2":[1,2],"-3":[1]}
move_right
3
0
3
1
j
1
2
1
3
2
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: {"0":[3,2,1],"1":[2,1],"2":[1],"-1":[3,1],"-2":[1,2],"-3":[1]}
fill_cells
3
0
3
1
i
1
2
j
1
3
2
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: {"0":[3,2,1],"1":[2,1],"2":[1],"-1":[3,1,2],"-2":[1,2],"-3":[1]}
record
j
1
0
3
1
1
2
1
3
2
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: [[1,3,1,1],[2,2,1,2],[1,1,1,2]]
record
i
1
0
j
1
1
1
2
1
3
2
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: [[1,1,1,1],[2,2,1,2],[1,1,1,2]]
record
i
1
0
1
1
j
1
2
1
3
2
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: [[1,1,1,1],[2,2,1,2],[1,1,1,2]]
record
i
1
0
1
1
1
2
j
1
3
2
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: [[1,1,1,1],[2,2,1,2],[1,1,1,2]]
record
j
1
0
i
1
1
1
2
1
3
1
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: [[1,1,1,1],[1,2,1,2],[1,1,1,2]]
record
1
0
j
1
1
1
2
1
3
1
4
2
5
1
6
2
7
1
8
1
9
1
10
2
11
Result: [[1,1,1,1],[1,2,1,2],[1,1,1,2]]
record
1
0
i
1
1
j
1
2
1
3
1
4
2
5
2
6
2
7
1
8
1
9
1
10
2
11
Result: [[1,1,1,1],[1,2,2,2],[1,1,1,2]]
record
1
0
i
1
1
1
2
j
1
3
1
4
2
5
2
6
2
7
1
8
1
9
1
10
2
11
Result: [[1,1,1,1],[1,2,2,2],[1,1,1,2]]
record
j
1
0
1
1
i
1
2
1
3
1
4
2
5
2
6
2
7
1
8
1
9
1
10
2
11
Result: [[1,1,1,1],[1,2,2,2],[1,1,1,2]]
record
1
0
j
1
1
i
1
2
1
3
1
4
2
5
2
6
2
7
1
8
2
9
1
10
2
11
Result: [[1,1,1,1],[1,2,2,2],[1,2,1,2]]
record
1
0
1
1
j
1
2
1
3
1
4
2
5
2
6
2
7
1
8
2
9
3
10
2
11
Result: [[1,1,1,1],[1,2,2,2],[1,2,3,2]]
record
1
0
1
1
i
1
2
j
1
3
1
4
2
5
2
6
2
7
1
8
2
9
3
10
3
11
Result: [[1,1,1,1],[1,2,2,2],[1,2,3,3]]

Key Takeaways

✓ Each diagonal is identified by the key (row - column) and processed independently.

This insight is hard to see from code alone because the diagonal key calculation is subtle and not explicitly visualized.

✓ Using min-heaps for each diagonal allows efficient sorting and retrieval of the smallest elements.

Watching heaps fill and then pop elements clarifies how sorting is done without sorting the entire matrix.

✓ The two-pass traversal first builds heaps, then replaces matrix elements, ensuring diagonals are sorted in-place.

Seeing the pointer movements and heap operations step-by-step reveals the algorithm's structure and flow.

Practice

(1/5)
1. Given the following code for computing the h-index, what is the returned value when the input is [4, 1, 0, 5, 6]?
easy
A. 3
B. 4
C. 5
D. 2

Solution

  1. Step 1: Sort citations descending

    Sorted list: [6,5,4,1,0]
  2. Step 2: Binary search on h from 0 to 5

    Check mid=2: count papers >=2 is 3 (6,5,4) ≥2 -> low=3; mid=4: count ≥4 is 3 -> low=5; mid=5: count ≥5 is 2 -> high=4; loop ends with high=4.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Final h-index is 4 for this input [OK]
Hint: Count papers ≥ mid after sorting [OK]
Common Mistakes:
  • Off-by-one in binary search boundaries
  • Not breaking early in count loop
  • Returning low instead of high
2. Consider the following Python code for finding the maximum gap in a list of integers. What is the value of max_gap after the second iteration of the for-loop when the input is [3, 6, 9, 1]?
from typing import List

def maximumGap(nums: List[int]) -> int:
    if len(nums) < 2:
        return 0
    min_num, max_num = min(nums), max(nums)
    if min_num == max_num:
        return 0
    nums.sort()
    max_gap = 0
    for i in range(1, len(nums)):
        max_gap = max(max_gap, nums[i] - nums[i-1])
    return max_gap

print(maximumGap([3,6,9,1]))  # Output: ?
easy
A. 3
B. 5
C. 6
D. 8

Solution

  1. Step 1: Sort the input array

    Input: [3,6,9,1] -> Sorted: [1,3,6,9]
  2. Step 2: Trace max_gap updates in the loop

    Iteration 1 (i=1): max_gap = max(0, 3-1) = 2 Iteration 2 (i=2): max_gap = max(2, 6-3) = 3
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    max_gap after second iteration is 3 [OK]
Hint: Sort first, then scan adjacent differences [OK]
Common Mistakes:
  • Off-by-one errors in loop
  • Using unsorted array differences
  • Confusing iteration indices
3. You are given a sorted list of unique integers and a range defined by lower and upper. You need to find all the missing continuous ranges within this interval that are not present in the list. Which algorithmic approach best guarantees an optimal solution in terms of time complexity?
easy
A. Sorting the list and then scanning linearly to find gaps
B. Using binary search to quickly locate missing ranges between elements
C. Dynamic programming to build missing ranges from subproblems
D. Greedy approach scanning the entire range and adding missing numbers one by one

Solution

  1. Step 1: Understand problem constraints

    The problem requires finding missing ranges efficiently within a sorted list and a given interval.
  2. Step 2: Identify approach that leverages sorted data

    Since the list is already sorted, scanning linearly to find gaps between consecutive elements is optimal and straightforward.
  3. Step 3: Evaluate binary search approach

    Using binary search to locate missing ranges is unnecessary overhead because the list is sorted and unique; a linear scan suffices.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Linear scan over sorted unique list is O(n), optimal for this problem [OK]
Hint: Linear scan over sorted unique list is optimal for missing ranges [OK]
Common Mistakes:
  • Assuming binary search is needed despite sorted input
  • Confusing DP with range search
  • Ignoring linear scan benefits
4. Consider the following Python code implementing the optimal approach for relative sorting. Given arr1 = [3,1,2,2] and arr2 = [2,3], what is the final returned list?
easy
A. [2, 3, 1, 2]
B. [3, 2, 2, 1]
C. [2, 2, 3, 1]
D. [1, 2, 2, 3]

Solution

  1. Step 1: Build frequency array for arr1

    arr1 = [3,1,2,2], max_val=3, freq = [0,1,2,1] (indices 0 to 3)
  2. Step 2: Construct result using arr2 order then leftovers

    Output elements for arr2: 2 appears twice -> [2,2], 3 appears once -> [2,2,3]. Leftover is 1 once -> [2,2,3,1]
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Frequency counts and order match output [OK]
Hint: Trace frequency array and output order carefully [OK]
Common Mistakes:
  • Appending leftover elements before arr2 order
  • Miscounting frequencies or off-by-one in freq array
  • Mixing order of arr2 elements in output
5. What is the time complexity of the optimal approach that sorts the array and scans adjacent pairs to find the minimum absolute difference?
medium
A. O(n) because scanning adjacent pairs is linear
B. O(n²) because every pair is compared
C. O(n log n) due to sorting, scanning is O(n)
D. O(log n) because sorting uses divide and conquer

Solution

  1. Step 1: Identify sorting cost

    Sorting an array of size n takes O(n log n) time.
  2. Step 2: Identify scanning cost

    Scanning adjacent pairs is a single pass O(n).
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Sorting dominates total time -> O(n log n) [OK]
Hint: Sorting dominates; scanning is linear [OK]
Common Mistakes:
  • Confusing scanning cost as sorting cost
  • Assuming O(n) overall without sorting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard