"Sort the Students by Their Kth Score" is a matrix-based sorting problem. You are given a 2D integer matrix where each row represents a student and each column represents a specific exam score. You are also given an integer .
Your task is to sort the entire rows of the matrix in descending order based on the values in the -th column (the -th exam). This is a practical example of record sorting, where you reorder a collection of objects (students) based on a specific attribute (one of their exam scores).
Companies like IBM and Amazon ask this interview question to test a candidate's ability to perform custom sorting on complex data structures. It evaluates whether you can manipulate 2D arrays (matrices) and whether you understand how to use custom comparators in your programming language. It’s a common task in data engineering and backend development, where you often need to sort records based on a user-selected field.
The primary algorithmic pattern is Custom Sorting.
key or a comparator to the built-in sort function, making this an operation, where is the number of rows.Matrix: [[10, 6, 9, 1], [7, 5, 11, 2], [4, 8, 3, 15]] k = 2 (the 3rd exam, 0-indexed).
One frequent mistake is sorting the columns instead of the rows, which garbles the student data. Another mistake is sorting in ascending order instead of the requested descending order. Some candidates also try to manually implement a sorting algorithm like Bubble Sort, which is inefficient () and unnecessary when high-performance built-in sorting utilities are available.
To master the "Sort the Students by Their Kth Score coding problem," practice using lambda functions or anonymous comparators. In Python, matrix.sort(key=lambda x: x[k], reverse=True) is the most idiomatic way. In Java, you would use Arrays.sort(matrix, (a, b) -> b[k] - a[k]). Being comfortable with these one-liners shows you have a strong grasp of the language's utility functions.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Sort Matrix by Diagonals | Medium | Solve | |
| Sort the Matrix Diagonally | Medium | Solve | |
| Largest Submatrix With Rearrangements | Medium | Solve | |
| Minimum Operations to Make a Uni-Value Grid | Medium | Solve | |
| Best Meeting Point | Hard | Solve |