Valtech interview question

traverse the matrix