FastPrepSort Concentric Matrix Rings
Problems/Sort Concentric Matrix Rings

Sort Concentric Matrix Rings

xAI logoxAI● MediumFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The assessment report describes sorting each concentric square border clockwise from its top-left corner. The traversal and output rules below make the task something you can run, without changing its main behavior. Our version is about 95% the same as the reported question.

Given an n x n integer matrix, process each concentric ring independently. A ring is identified by its layer k, whose corners are (k, k) and (n - 1 - k, n - 1 - k).

Traverse a non-singleton ring clockwise beginning at its top-left cell: across the top edge, down the right edge, across the bottom edge from right to left, and up the left edge. Visit every ring cell exactly once. Sort that ring's values in nondecreasing order and write them back along the same traversal. If n is odd, the center cell is a singleton ring and remains unchanged.

Return the transformed matrix. Do not mix values between different rings.

Function

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

Examples

Example 1

matrix = [[9,1,8],[2,5,7],[6,3,4]]return = [[1,2,3],[9,5,4],[8,7,6]]
matrix
012091812572634
→
return
012012319542876
Moved or new

The outer clockwise traversal contains [9,1,8,7,4,3,6,2]. Writing its sorted values [1,2,3,4,6,7,8,9] along the same path gives the result; the center 5 stays fixed.

Example 2

matrix = [[4,1],[3,2]]return = [[1,2],[4,3]]
matrix
01041132
→
return
01012143
Moved or new

The only ring is traversed as [4,1,2,3], then replaced by [1,2,3,4].

Constraints

  • 1 <= n <= 200.
  • matrix.length == matrix[i].length == n.
  • -10^9 <= matrix[i][j] <= 10^9.

More xAI interview questions

  • Largest Plus SignOA · Seen May 2025
  • Longest Common SubstringOA · Seen May 2025
  • Resumable List IteratorPHONE SCREEN · Seen Apr 2026
  • Earliest Dataset Readiness in a PipelinePHONE SCREEN · Seen Mar 2026
  • Live Top-K Spaces by Active UsersONSITE INTERVIEW · Seen Jan 2026
  • Total Active Time by SpaceONSITE INTERVIEW · Seen Jan 2026
  • Dynamic Batch Decode TraceONSITE INTERVIEW · Seen Dec 2025
  • Nested Transaction Key-Value StoreONSITE INTERVIEW · Seen Oct 2025
  • Weighted LRU CacheONSITE INTERVIEW · Seen Oct 2025
  • LFU CacheONSITE INTERVIEW · Seen Sep 2025

Similar questions at other companies

  • Sort Every Matrix Border LayerCapital One · OACapital One interview questions
  • Sort Concentric Matrix Borders ClockwiseByteDance · OAByteDance interview questions
  • Simulate Orchard Rot SpreadAirbnb · OAAirbnb interview questions
  • Subscription and Usage-Based Billing CalculatorStripe · OAStripe interview questions
  • Capping Portfolio WeightsCapstone Investment Advisors · OA
  • Rotate Matrix Regions Around the DiagonalsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
  • Minimize Task Resource ConsumptionNVIDIA · OANVIDIA interview questions
  • Build Sessions from Unordered Events (Part 1)Scale AI · OAScale AI interview questions
  • Macaron Order PriceOpenAI · OAOpenAI interview questions
  • Rotate Matrix Regions Around the DiagonalsHudson River Trading · OAHudson River Trading interview questions

xAI interview experiences

  • xAI New Grad AI Coding Interview and Take-Home: 3 Experiences
All xAI interview questionsSee xAI hiring insights
Saved
class Solution {
    public int[][] sortConcentricRings(int[][] matrix) {
        // Write your code here.
    }
}
Checking account…