FastPrepSort Concentric Matrix Borders Clockwise
Problems/Sort Concentric Matrix Borders Clockwise

Sort Concentric Matrix Borders Clockwise

ByteDance logoByteDance● MediumNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report does not show the example output, does not say how to walk the inner border of a very small matrix, and gives no number limits or function format for a language. Our version is about 93% the same as the interview report.

Given a rectangular integer matrix, divide it into concentric borders by repeatedly removing the current outer border.

For each border independently, collect its values in clockwise order starting at that border's top-left cell, sort the collected values in ascending order, and write them back along the same traversal.

Traverse a nondegenerate border across the top edge from left to right, down the right edge, across the bottom edge from right to left, and up the left edge. Visit every cell exactly once. Traverse a one-row border from left to right and a one-column border from top to bottom.

Return the resulting matrix.

Function

sortMatrixBorders(matrix: int[][]) → int[][]

Examples

Example 1

matrix = [[9, 7, -4, 5], [1, 6, 2, -6], [12, 20, 2, 0]]return = [[-6, -4, 0, 1], [20, 2, 6, 2], [12, 9, 7, 5]]
matrix
0123097-451162-62122020
→
return
01230-6-401120262212975
Moved or new

The outer border is sorted and rewritten clockwise as [-6, -4, 0, 1, 2, 5, 7, 9, 12, 20]. The inner one-row border [6, 2] becomes [2, 6].

Example 2

matrix = [[3], [1], [2]]return = [[1], [2], [3]]
matrix
0031122
→
return
0011223
Moved or new

The only border is one column, so it is visited from top to bottom and sorted in that order.

Constraints

  • 1 <= matrix.length <= 200
  • 1 <= matrix[i].length <= 200
  • Every row has the same length.
  • -10^9 <= matrix[i][j] <= 10^9

More ByteDance interview questions

  • Sorted Absolute-Difference Sums Across Cyclic ShiftsOA · Seen Aug 2026
  • Count House Segments After Sequential DestructionOA · Seen Aug 2026
  • Count Numbers with an Even Digit CountOA · Seen Aug 2026
  • Largest Number Below N from Reusable DigitsPHONE SCREEN · Seen Nov 2026
  • Log Analyzer: Busiest Service in a Rolling WindowPHONE SCREEN · Seen Nov 2026
  • Search a Row- and Column-Sorted MatrixONSITE INTERVIEW · Seen Nov 2026
  • Majority ElementPHONE SCREEN · Seen Nov 2026
  • Valid ParenthesesPHONE SCREEN · Seen Nov 2026
  • Binary Tree Target-Sum PathsPHONE SCREEN · Seen Nov 2026
  • Longest Increasing SubsequencePHONE SCREEN · Seen Nov 2026

Similar questions at other companies

  • Capping Portfolio WeightsCapstone Investment Advisors · OA
  • Minimize Task Resource ConsumptionNVIDIA · OANVIDIA interview questions
  • Macaron Order PriceOpenAI · OAOpenAI interview questions
  • Maximum Diamond SumHudson River Trading · OAHudson River Trading interview questions
  • Robot Navigation Around LasersRoblox · OARoblox interview questions
  • Sorted Cyclic Shift Difference SumsCapital One · OACapital One interview questions
  • Optimize Package Order by Sorting One WindowAmazon · OA
  • Flip, Invert, and Smooth a Binary ImageAbridge · OAAbridge interview questions
  • First Completed Mingo LineEpic · OAEpic interview questions
  • Rotate a Square Matrix ClockwiseVisa · OAVisa interview questions

ByteDance interview experiences

  • ByteDance TikTok USDS SWE Intern Interview: 1 Experience
  • ByteDance MLE New Grad Interviews: 1 Experience
  • ByteDance Security SRE Intern Phone Screens: 1 Experience
  • ByteDance USDS New Grad SWE Phone Screen: 1 Experience
  • ByteDance New Grad Data Engineer Video Interviews: 1 Experience
  • ByteDance Trust & Safety Research Scientist Intern: 1 Experience
All ByteDance interview questionsSee ByteDance hiring insights
Saved
class Solution {
    public int[][] sortMatrixBorders(int[][] matrix) {
        // Write your code here.
    }
}
Checking account…