FastPrepTrace of a Square Matrix
Problems/Trace of a Square Matrix

Trace of a Square Matrix

Akuna Capital logoAkuna Capital● EasyNEW GRADINTERNPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reports ask for the trace or diagonal sum as part of matrix operations. We chose these rules: it means the main diagonal, integers have limits, the empty case has a defined result, and the input is promised to be valid. We kept the follow-up about checking input that the reports mention.

Our version is about 95% the same as the reported questions.

Given a square integer matrix matrix, return its trace: the sum of the main-diagonal entries from the top-left corner to the bottom-right corner.

In zero-based indexing, add matrix[i][i] for every row i. Do not add the other diagonal. The trace of an empty matrix is 0.

Inputs satisfy the constraints below. Practice follow-up: explain which shape and value checks you would perform before taking the trace of a general nested input. No malformed-input result is graded here.

Function

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

Examples

Example 1

matrix = [[3,8],[5,-2]]return = 1
matrix
0103815-2

The main diagonal is 3 and -2, so the trace is 3 + (-2) = 1.

Example 2

matrix = [[7]]return = 7
matrix
007

The only entry, 7, is also the only main-diagonal entry.

Example 3

matrix = []return = 0

The empty matrix has no diagonal entries, so their sum is 0.

Constraints

  • 0 <= matrix.length <= 1000
  • matrix is square: every row has exactly matrix.length entries.
  • -1000000 <= matrix[i][j] <= 1000000

More Akuna Capital Phone Screen questions

  • Match Orders by Price and Arrival TimeOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Biased Coin: Probability of Exactly h HeadsPHONE SCREEN · Seen Nov 2026
  • Positive Nth Root with Gradient DescentPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Two SumOA · PHONE SCREEN · Seen Sep 2026
  • Maximum Value in a Binary TreePHONE SCREEN · Seen Sep 2026
  • Tiered Expiring Item StorePHONE SCREEN · Seen Aug 2026
  • Statistics for the Latest K ValuesPHONE SCREEN · Seen Nov 2025
  • Remove Duplicates While Preserving OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2025
  • Sliding Window MaximumPHONE SCREEN · Seen Oct 2025
  • Evaluate a PolynomialPHONE SCREEN · Seen Oct 2025

Similar questions at other companies

  • Rotate a Matrix by 90 DegreesIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
  • Validate a Chess Bishop, Rook, or Queen MoveGlean · PHONE SCREENGlean interview questions
  • Rotate a Flat Image ClockwiseGoogle · ONSITE INTERVIEWGoogle interview questions
  • Matrix MultiplicationeBay · ONSITE INTERVIEWeBay interview questions
  • One-Nearest-Neighbor Classification with Manhattan DistanceOpenAI · PHONE SCREENOpenAI interview questions
  • Unique Pairs in a 2D Matrix Summing to TargetAmazon · PHONE SCREEN
  • Map Black Cells to Covering 2x2 BlocksSquare · PHONE SCREEN
  • Traverse Interior Matrix Cells in Row-Major OrderCitadel · ONSITE INTERVIEWCitadel interview questions
  • Trace an Eight-Direction Robot Until BlockedPinterest · PHONE SCREENPinterest interview questions
  • Detect a Winner on a 3x3 Tic-Tac-Toe BoardHebbia · ONSITE INTERVIEW

Akuna Capital interview experiences

  • Akuna Capital Quant Trader and QR Interviews: 5 Experiences
  • Akuna Capital JQR CodePair Rounds: 8 Experiences (Part 6 of 6)
  • Akuna Capital QR Probability Phones: 14 Experiences (Part 5 of 6)
  • Akuna Capital QR-ML Interviews: 11 Experiences (Part 4 of 6)
  • Akuna Capital QR Regression Rounds: 11 Experiences (Part 3 of 6)
  • Akuna Capital QR Coding Interviews: 17 Experiences (Part 2 of 6)
All Akuna Capital interview questionsSee Akuna Capital hiring insights
Saved
class Solution {
    public int matrixTrace(int[][] matrix) {
        // write your code here
    }
}
Checking account…