Sudoku Validity Problem Statement

You are provided with a 9 x 9 2D matrix MATRIX containing digits (1-9) and empty cells indicated by 0.

Your task is to determine if there exists a method to fill all empty cells with digits (1-9) such that the resulting matrix is a valid Sudoku solution.

A valid Sudoku solution must meet the following criteria:

  • Each digit from 1 to 9 must appear exactly once in each row.
  • Each digit from 1 to 9 must appear exactly once in each column.
  • Each digit from 1 to 9 must appear exactly once in each of the nine 3 x 3 sub-matrices within the main matrix.

Input:

The first line contains an integer 'T', the number of test cases. For each test case, 9 lines follow, each containing 9 space-separated integers representing a row in the matrix.

Output:

For each test case, output "yes" if a valid Sudoku solution exists, or "no" if it does not. Outputs should be in lowercase.

Example:

Input:
2
5 3 0 0 7 0 0 0 0
6 0 0 1 9 5 0 0 0
0 9 8 0 0 0 0 6 0
8 0 0 0 6 0 0 0 3
4 0 0 8 0 3 0 0 1
7 0 0 0 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9
8 3 5 4 1 9 2 7 6
7 2 6 8 5 0 4 9 1
5 9 1 7 3 2 0 6 8
4 6 8 2 7 3 5 1 9
6 7 2 3 9 1 8 5 4
3 8 9 5 6 4 1 2 7
1 5 4 9 8 7 6 3 2
Output:
yes
no
  • The matrix always contains at least one empty cell.
  • The initial matrix is consistent with Sudoku rules.

Constraints:

  • 1 ≤ T ≤ 5
  • N = 9
  • 0 ≤ MATRIX[i][j] ≤ 9
  • Time Limit: 1 sec
Be the first one to answer
Add answer anonymously...
JPMorgan Chase & Co. Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter