Magicsheet logo

The K Weakest Rows in a Matrix

Easy
25%
Updated 8/1/2025

The K Weakest Rows in a Matrix

What is this problem about?

Matrix manipulation is a staple of technical interviews, and "The K Weakest Rows in a Matrix" provides a unique twist by defining row "strength." You are given a binary matrix where each row consists of a sequence of ones followed by a sequence of zeros. The number of ones represents the strength of the row. A row is considered "weaker" than another if it has fewer ones. If two rows have the same number of ones, the row with the lower index is considered the weaker of the two. The goal is to return the indices of the 'k' weakest rows in order from weakest to strongest.

Why is this asked in interviews?

This the K Weakest Rows in a Matrix interview question is asked by Microsoft, Meta, Amazon, and Google because it touches on several fundamental computer science concepts: binary search, sorting, and priority queues. Because each row is sorted (ones then zeros), you don't need to count the ones linearly; you can find the transition point more efficiently. It tests whether a candidate can identify these internal optimizations and handle multi-criteria sorting effectively.

Algorithmic pattern used

The most efficient approach for this Array, Matrix, Sorting, Binary Search, Heap (Priority Queue) interview pattern involves two distinct steps. First, calculate the number of ones in each row. Since the rows are sorted, you can use binary search to find the first occurrence of a zero, which gives you the count of ones in O(log n) time per row. Second, you need to find the smallest 'k' values. You can do this by creating a list of (count, index) pairs and sorting them, or more optimally, by using a max-heap of size 'k'. A max-heap allows you to keep track of the weakest rows seen so far and discard the strongest among them as you iterate through the matrix, resulting in a very efficient selection process.

Example explanation

Imagine a 3x3 matrix: Row 0: [1, 1, 0] (Strength 2) Row 1: [1, 0, 0] (Strength 1) Row 2: [1, 1, 1] (Strength 3) If k = 2:

  1. Binary search on Row 0 finds the first 0 at index 2. Count = 2.
  2. Binary search on Row 1 finds the first 0 at index 1. Count = 1.
  3. Binary search on Row 2 finds no 0. Count = 3. The strengths are (2, 0), (1, 1), and (3, 2). Sorting these by strength then index: (1, 1) < (2, 0) < (3, 2). The weakest k=2 indices are [1, 0].

Common mistakes candidates make

In "The K Weakest Rows in a Matrix coding problem," a common mistake is using a simple sum() function to count ones in each row. While correct, it is O(n) per row, whereas binary search is O(log n), which is a significant difference for wide matrices. Another mistake is failing to correctly implement the tie-breaking rule (row index) during the sorting or heap comparison phase. Finally, some candidates might return the strengths themselves instead of the requested row indices.

Interview preparation tip

When you see a problem involving "sorted rows" or "sorted portions" of an array, your first instinct should be to check if binary search is applicable. For selecting the 'top k' or 'bottom k' elements, always consider if a heap (priority queue) can improve your time complexity over a full sort. Practice implementing heaps in your favorite language, as they are essential for many optimization problems.

Similar Questions