1

Matrix Trace

Unsolved
Linear Algebra

Difficulty: 3 | Problem written by mesakarghm
Problem reported in interviews at

Amazon
Apple
Facebook
Google
Netflix

The trace of a square matrix A, denoted as tr(A), is the sum of its diagonal entries. 

\(tr(A) = \sum_{i=1} ^ {n} A_{i,i}\)

Write a function trace_mat(mat) which calculates and returns the trace of a given square matrix. 

The function has to return None if the given matrix isn't square. 

Sample Input:
<class 'list'>
mat: [[17, 24, 15, 58, 15], [23, 55, 47, 114, 216], [4, 6, 13, 220, 22], [10, 12, 19, 21, 3], [11, 18, 25, 12, 19]]

Expected Output:
<class 'int'>
125

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Adipisci ab aut suscipit reprehenderit placeat quae similique ipsum, molestias reiciendis laudantium? Corrupti labore provident voluptas quia.

Tempora dolore quod quae beatae blanditiis doloremque perspiciatis aperiam perferendis corporis consectetur, id iste illo explicabo nemo, corrupti placeat reprehenderit pariatur inventore voluptas laborum est quo autem, tenetur a non quod delectus rerum eum iusto? Ea voluptatum quas est id impedit, distinctio quibusdam ipsa numquam, quia illo enim consectetur doloribus animi, adipisci aliquam asperiores perspiciatis voluptatibus cumque commodi harum quis sed? Asperiores similique distinctio voluptate qui pariatur hic minima obcaecati dolorem eaque, asperiores laboriosam temporibus eum?

Voluptatum quidem aperiam repellat minima libero voluptates, beatae ratione suscipit doloremque quo distinctio vitae labore commodi iste, ducimus laboriosam ipsa quis quas odio nobis quod, optio hic reprehenderit est error beatae dolore laborum repellat nulla numquam accusamus, fugiat dolor provident aut ab quisquam?

This is a premium feature.
To access this and other such features, click on upgrade below.

Ready.

Input Test Case

Please enter only one test case at a time
numpy has been already imported as np (import numpy as np)