Sudoku
A Backtracking Problem

Algorithms and Data Structures: TheAlgorist.com

System Design: SystemsDesign.Cloud

Low Level Design: LowLevelDesign.io

Frontend Engineering: FrontendEngineering.io
Problem Statement:
Given a Sudoku, design an algorithm to solve it.
A sudoku solution must satisfy all of the following rules:
 Each of the digits 19 must occur exactly once in each row.
 Each of the digits 19 must occur exactly once in each column.
 Each of the digits 19 must occur exactly once in each of the 9 3x3 subboxes of the grid.
The '0' character indicates empty cells.
Solution:
 NOTE: I highly recommend going through the Backtracking chapters in the order they are given in the Index page to get the most out of it and be able to build a rocksolid understanding.
This is one of my personal favorites. Solving this problem shows the power and advantage of knowing the basic algorithms really good.
If someone is asked to design an algorithm with working code in a 40 mins coding interview, it might seem daunting. But here we would see how if you understand how backtracking works you would be able to come up with a very simple yet elegant algorithm with working code in under 30 minutes.
The concept is simple: we do an exhaustive search. For every cell that does not contain '0' we compute what are the values that cell can contain, and for each and every valid candidate we recursively call backtrack(...) to see if we can get a valid solution. Once we get the first valid solution the process stops.
We will be using our backtracking template discussed in Backtracking Fundamentals to write our code.
Java implementation:
This is a Premium content.
Please subscribe to Algorithms course to access the code.
Python implementation:
This is a Premium content.
Please subscribe to Algorithms course to access the code.
Don't forget to take indepth look at the other backtracking problems because that is what would make you comfortable with using the backtracking template and master the art of Backtracking:
 Letter Case Permutation
 Power Set
 All Paths Between Two Nodes
 Word Search
 NQueens
 Word Square
 Generate Parentheses
Instructor:
If you have any feedback, please use this form: https://thealgorists.com/Feedback.
Follow Us On LinkedIn