Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

101. Symmetric matrices

easy
GeneralGeneral
senior

Work with symmetric matrices by checking whether a square matrix equals its transpose and, if it is symmetric, returning its symmetric part.

A=A⊤,S=12(A+A⊤)A = A^\top, \quad S = \frac{1}{2}(A + A^\top)

Requirements

Implement the function

python

Rules:

  • Treat A as symmetric if the maximum absolute entry-wise difference between A and A^T is at most 1e-9.
  • Always return the symmetric part (S = \frac{1}{2}(A + A^\top)), even if A is not symmetric.
  • Input and output must be NumPy arrays.
  • Keep it all inside this single function.

Example

python

Output:

python
Input Signature
ArgumentType
Anp.ndarray
Output Signature
Return NameType
valuetuple

Constraints

  • Input/output must be NumPy arrays.

  • Square matrix; use tolerance 1e-9.

Hint 1

Symmetry check: compare A with A.T using np.abs(A - A.T) and find the maximum difference.

Hint 2

Use the tolerance rule: is_symmetric = (max_diff <= 1e-9).

Hint 3

Compute symmetric part vectorized: S = 0.5 * (A + A.T).

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
senior
entry
Tags
matrix-transpose
numerical-tolerance
symmetric-part
nested-loops
36 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit