Sort Every Top-Left to Bottom-Right Diagonal of a Score Grid
Last updated: November 29, 2026
Quick Overview
Given an R x C grid of scores, sort each diagonal running from top-left to bottom-right in ascending order and return the grid.
Meta
November 29, 20260
0
0 solved
Given an R x C grid of scores, sort each diagonal running from top-left to bottom-right in ascending order and return the grid.
A Meta phone screen question about grouping by an invariant key. It can be framed as normalizing a ranking table where each diagonal is one cohort tracked across days. Interviewers check that you recognize `r - c` as the diagonal id and write back in order without extra index bookkeeping.
What the Interviewer Expects
- Identify r - c as a constant along each diagonal
- Group values by that key
- Sort each group and write values back in row-major order
- Explain why row-major order visits each diagonal top to bottom
- Discuss counting sort when values are small
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Sort the anti-diagonals instead.
- How would you do it with O(min(R, C)) extra space?
- Values are in 1..100; can you avoid comparison sorting?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Grid up to 100 x 100 with values 1..100.
- Each diagonal is sorted independently in ascending order from top-left to bottom-right....
High-Level Design
Every cell on a top-left to bottom-right diagonal has the same r - c, so bucket values by that key. Sort each bucket. When we scan the grid again in row-major order, cells on a given diagonal are vi...