Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Implement a function to return the sum of diagonal elements in a square matrix.

Medium

Problem Statement

Implement a function to return the sum of diagonal elements in a square matrix.

Sample Input

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Sample Output

15

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
import java.util.*; public class Main { public static int diagonalSum() { int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Your code here return 0; } }
Log in or sign up to run or submit your code.Log inSign up
Input
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Expected Output
15