Skip to content

Conversation

@Navadeep0007
Copy link
Contributor

Implemented Sudoku Solver using backtracking algorithm to solve 9x9 Sudoku puzzles.

Changes

  • Added SudokuSolver.java with backtracking implementation
  • Added SudokuSolverTest.java with comprehensive test cases
  • Validates row, column, and 3x3 subgrid constraints
  • Handles edge cases (null board, invalid size, empty board)

Algorithm Explanation

Uses backtracking to:

  1. Find empty cells (marked as 0)
  2. Try digits 1-9 in each empty cell
  3. Check if placement is valid (not in row/column/subgrid)
  4. Recursively solve remaining cells
  5. Backtrack if no valid solution found

Time Complexity: O(9^(nn)) worst case
Space Complexity: O(n
n) for recursion stack

@codecov-commenter
Copy link

codecov-commenter commented Nov 15, 2025

Codecov Report

❌ Patch coverage is 73.80952% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.34%. Comparing base (c6880c1) to head (8931014).

Files with missing lines Patch % Lines
...a/com/thealgorithms/backtracking/SudokuSolver.java 73.80% 10 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7073      +/-   ##
============================================
- Coverage     78.35%   78.34%   -0.02%     
- Complexity     6689     6714      +25     
============================================
  Files           756      757       +1     
  Lines         22203    22245      +42     
  Branches       4365     4380      +15     
============================================
+ Hits          17398    17427      +29     
- Misses         4106     4117      +11     
- Partials        699      701       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Navadeep0007
Copy link
Contributor Author

@DenizAltunkapan could you please review into my PR,
I appreciate your efforts!

@DenizAltunkapan
Copy link
Member

DenizAltunkapan commented Nov 16, 2025

@Navadeep0007 what is the difference between your implementation and the existing one?https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/puzzlesandgames/Sudoku.java

@Navadeep0007
Copy link
Contributor Author

@DenizAltunkapan Thank you for pointing out the existing implementation! I've reviewed
both and here are the key differences in my PR:

Advantages of my implementation:

  1. Better package location: Mine is in backtracking package (more appropriate for
    the algorithm type) vs puzzlesandgames
  2. Input validation: My implementation validates null boards and incorrect dimensions;
    the existing code has no validation
  3. Test coverage: I've added comprehensive unit tests covering edge cases (null board,
    invalid size, empty board, already solved) - the existing implementation has no tests
  4. Code readability: Separated validation logic into distinct methods (isNumberInRow,
    isNumberInColumn, isNumberInSubgrid) with meaningful constants
  5. Clearer documentation: Each method is well-documented with specific Sudoku context

Trade-off:

  • The existing implementation supports any n×n Sudoku (flexible), while mine is
    specifically optimized for standard 9×9 puzzles (most common use case)

I believe the test coverage alone adds significant value to the repository. Would you
prefer me to:

  1. Keep this PR as-is (adds a well-tested 9×9 specific solver in the backtracking package)
  2. Enhance the existing Sudoku.java with my input validation and test cases
  3. Merge both approaches (keep existing flexibility but add my validation and tests)

Happy to adjust based on your preference!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants