Sorted Extended Matrix Diagonals
Reported by candidates from Capital One's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The hinted pattern here is breadth-first-search, but the real work in this Capital One question, reported in September 2026, is building strings and sorting them. You get an n x n character matrix, pull out 2n - 1 diagonals, repeat each one cyclically until it's n characters long, then return the 1-based indices in sorted order. Ties keep their original order. Nothing here needs a queue or a graph. It's a matrix traversal plus a stable sort. If you blank on the indexing, StealthCoder runs invisibly during the live OA and gives you a working solution to check against.
The problem
Given a square matrix of characters matrix with a size of n x n, your task is to create a sorted list of matrix's extended diagonals, where each diagonal has a length of n. A matrix of size n x n has 2n - 1 diagonals parallel to the main diagonal, with each diagonal starting at its upper point and ending at its lower point. Since these diagonals have different lengths, traverse each one cyclically (go back to the starting point after reaching the end point) until it consists of n characters. Sort the resulting strings of n characters in alphabetical order, and return an array of 2n - 1 integers, representing the diagonals' 1-based indices in their sorted order. In the case of alphabetically equal strings, their indices should be kept in the original order. Below you can find an example of diagonals' numbering for a 5 x 5 matrix, where the number in the matrix corresponds to the diagonal index: 5 6 7 8 9 4 5 6 7 8 3 4 5 6 7 2 3 4 5 6 1 2 3 4 5 Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse than O(n^4) will fit within the execution time limit. Function solution(matrix: String[][]) → int[] Examples Example 1 matrix = [["b","b"],["c","a"]] return = [2,3,1] For matrix = [["b", "b"], ["c", "a"]] the output should be solution(matrix) = [2, 3, 1]. The diagonal with index 1 is ["c"] and its corresponding cyclic string is "cc". The diagonal with index 2 is ["b", "a"] and its corresponding cyclic string is "ba". The diagonal with index 3 is ["b"] and its corresponding cyclic string is "bb". The alphabetical ordering of the matrix diagonals looks like ["ba", "bb", "cc"], so the answer is [2, 3, 1]. Example 2 matrix = [["a","c","a","b","b"],["c","b","a","c","b"],["a","a","e","c","b"],["b","b","d","a","g"],["a","b","e","b","a"]] return = [1,5,3,7,2,8,9,6,4] For matrix = [["a", "c", "a", "b", "b"], ["c", "b", "a", "c", "b"], ["a", "a", "e", "c", "b"], ["b", "b", "d", "a", "g"], ["a", "b", "e", "b", "a"]] the output should be solution(matrix) = [1, 5, 3, 7, 2, 8, 9, 6, 4]. The explanation for this example is not visible in the source image. Constraints FastPrep execution-adapter constraints (not shown in the source image): n = matrix.length and n >= 1. Every row of matrix has exactly n entries. Every entry of matrix is a one-character string.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is indexing. Diagonal k, from 1 to 2n - 1, starts at the bottom-left and ends at the top-right, with cells where j - i = k - n. Collect characters going down each diagonal in order, then repeat them cyclically until you have n characters. Build the string with a modulo over the diagonal's length, so a length-1 diagonal like "c" becomes "cc". Then sort the indices by string with a stable sort. The stated O(n^4) bound is generous. Building strings costs O(n^2) total and sorting is about O(n^2 log n) comparisons. The common pitfalls are off-by-one errors in the 1-based index, getting the diagonal order backwards, and using an unstable sort so equal strings swap places. Test Example 1 by hand first. StealthCoder is your hedge in the live OA if the index math falls apart under pressure.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Sorted Extended Matrix Diagonals cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Capital One's OA.
Capital One reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Sorted Extended Matrix Diagonals FAQ
What's the trick in the Capital One sorted extended diagonals problem?+
Build each diagonal as a string, extend it cyclically to length n, then sort the indices by those strings. The only hard part is mapping diagonal index to cells. Diagonal k holds cells where j - i equals k - n. After that it's a plain stable sort.
Is this really a breadth-first-search problem?+
No. The hint says BFS, but nothing here involves graph traversal or shortest paths. It's matrix traversal, string construction, and sorting. Don't waste time setting up a queue. A simple loop over each diagonal does the job.
How do I handle diagonals shorter than n?+
Take the diagonal's characters in order, then fill positions 0 to n - 1 using character at position p mod length. A single-cell diagonal "c" becomes "cc" in a 2x2 matrix. A two-cell diagonal "ba" in a 2x2 matrix stays "ba".
How do I keep equal strings in original order?+
Use a stable sort keyed on the string, with indices 1 to 2n - 1 as the items. In languages with unstable sorts, sort by the pair of string and index. Either way, ties resolve to the smaller index first, which matches the required output.
How should I prepare in 48 hours for this Capital One OA?+
Practice diagonal indexing on a small matrix until you can write it without errors. Then rehearse Example 1 by hand. Also review stable sorting and custom comparators in your language. This question is about careful implementation, not a hard algorithm, so clean code beats clever code.