Bouncing Diagonals
Updated
Problem statement
Given a square matrix of integers matrix of size n x n, sort the values in its leftmost column by the weights of their bouncing diagonals and return them as a single array.
A bouncing diagonal starts from a cell of the leftmost column and moves diagonally up and to the right until it reaches the rightmost column. If it reaches the top of the matrix first, it bounces and keeps moving diagonally down and to the right.
The weight of a leftmost-column cell is the sum of the elements in the bouncing diagonal that starts from that cell.
Sort the leftmost-column values by their weights in ascending order. If two weights are equal, sort those values in ascending order too.
Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse than O(n^2) will fit within the execution time limit.
Function
solution(matrix: int[][]) → int[]Examples
Example 1
matrix = [[2,3,2],[0,2,5],[1,0,1]]return = [1,2,0]Cells are written as (row, column).
- The weight of the first element is
2 + 2 + 1 = 5, along(0,0) → (1,1) → (2,2). It starts on the top row, so it moves diagonally down and to the right from the start. - The weight of the second element is
0 + 3 + 5 = 8, along(1,0) → (0,1) → (1,2). It bounces off the top at(0,1). - The weight of the third element is
1 + 2 + 2 = 5, along(2,0) → (1,1) → (0,2).
The second element weight is greater than the others, so its value (0) goes to the end of the resulting array. There is a tie between the first and third elements, so they are sorted by their values (1, 2). The final order of the elements in the leftmost column is [1, 2, 0].
Example 2
matrix = [[1,3,2,5],[3,2,5,0],[9,0,1,3],[6,1,0,8]]return = [1,9,3,6]- The weight of the first element is
1 + 2 + 1 + 8 = 12, along(0,0) → (1,1) → (2,2) → (3,3). It starts on the top row, so it moves diagonally down and to the right from the start. - The weight of the second element is
3 + 3 + 5 + 3 = 14, along(1,0) → (0,1) → (1,2) → (2,3). It bounces off the top at(0,1). - The weight of the third element is
9 + 2 + 2 + 0 = 13, along(2,0) → (1,1) → (0,2) → (1,3). It bounces off the top at(0,2). - The weight of the fourth element is
6 + 0 + 5 + 5 = 16, along(3,0) → (2,1) → (1,2) → (0,3).
All the weights are different, and their ascending order is first, third, second, and fourth. So the final order of the elements in the leftmost column is [1, 9, 3, 6].
Example 3
matrix = [[1,8,4,3,4],[2,8,0,3,1],[0,7,9,0,8],[5,0,3,1,6],[1,5,0,3,1]]return = [2,5,1,1,0]- The weight of the first element is
1 + 8 + 9 + 1 + 1 = 20, along(0,0) → (1,1) → (2,2) → (3,3) → (4,4). It starts on the top row, so it moves diagonally down and to the right from the start. - The weight of the second element is
2 + 8 + 0 + 0 + 6 = 16, along(1,0) → (0,1) → (1,2) → (2,3) → (3,4). It bounces off the top at(0,1). - The weight of the third element is
0 + 8 + 4 + 3 + 8 = 23, along(2,0) → (1,1) → (0,2) → (1,3) → (2,4). It bounces off the top at(0,2). - The weight of the fourth element is
5 + 7 + 0 + 3 + 1 = 16, along(3,0) → (2,1) → (1,2) → (0,3) → (1,4). It bounces off the top at(0,3). - The weight of the fifth element is
1 + 0 + 9 + 3 + 4 = 17, along(4,0) → (3,1) → (2,2) → (1,3) → (0,4).
The second and fourth elements tie at 16, so their values come in ascending order: 2, then 5. Next come the weights 17, 20, and 23, so the answer is [2, 5, 1, 1, 0].
Constraints
2 <= matrix.length <= 500matrix[i].length == matrix.length0 <= matrix[i][j] <= 1000
More Airbnb Online Assessment questions
- Count Numbers Not Equal to the First TwoOA · Seen Dec 2026
- Garden Sprinkler PlacementOA · Seen Dec 2026
- Theme Park Shuttle Wait TimeOA · Seen Dec 2026
- Count Card Duel RoundsOA · Seen Nov 2026
- Count Lamps Lighting Each PointOA · Seen Nov 2026
- Simulate Orchard Rot SpreadOA · Seen Nov 2026
- Sort Adjacent PairsOA · Seen Nov 2026
- In-Memory File SystemOA · Seen Nov 2026
- Task Management SystemOA · Seen Nov 2026
- Recipe Management SystemOA · Seen Nov 2026
Similar questions at other companies
- Bouncing Diagonal WeightsCapital One · OACapital One interview questions
- Sort Concentric Matrix Borders ClockwiseByteDance · OAByteDance interview questions
- Sort Concentric Matrix RingsxAI · OAxAI interview questions
- Fill and Sort Packed Missing-Number GridsZipRecruiter · OAZipRecruiter interview questions
- Minimum Cost Path with TeleportationsAmazon · OA
- Best Meeting PointWalmart · OAWalmart interview questions
- Longest Increasing Path in a MatrixNutanix · OANutanix interview questions
- Sparse Matrix Addition and MultiplicationPinterest · PHONE SCREENPinterest interview questions
- Optimal Central WarehouseGoogle · ONSITE INTERVIEWGoogle interview questions
- Enumerate Character Paths to a MarkerTesla · ONSITE INTERVIEWTesla interview questions
Airbnb interview experiences
- Airbnb OA · November 2026
- Airbnb Campus and Full-Time SWE OA / Interviews: 9 Experiences
- Airbnb Intern Phone Interviews: 21 Experiences (Part 7 of 7)
- Airbnb Intern OA and Phone Screens: 17 Experiences (Part 6 of 7)
- Airbnb Intern OA and Onsite Rounds: 16 Experiences (Part 5 of 7)
- Airbnb Intern OA, Diplomacy Game: 19 Experiences (Part 4 of 7)