Search code examples
javaoopobject-oriented-analysis2d-games

Interview question: Create an object oriented design for Sudoku


I answered that I will have have a 2d Array.

And then I will have 3 functions

  • one to check the horizontal condition.
  • another function to check vertical condition
  • and another one the check the 3*3 block condition.

But he is not satisfied, can any one give a good answer for this question?

I found this stack overflow link related to my question. Programming Design Help - How to Structure a Sudoku Solver program?.

But I want a proper object oriented design (like what should be the classes, inheritance and other details) which are the same things interviewer expected from me.


Solution

  • To me, your design starts with a "region" class. You can then extend this to be a "horizontal region" "vertical region" and "square region" as the three types of regions. Edit: upon further consideration you don't really need to make this distinction unless it's for display purposes... algorithmically it will be the same.

    Then you can make your 2d array of "elements" and add the elements appropriately to your regions, which provides a network for your calculations. Your elements have a list of potential values, and your regions are responsible for removing those potential values. When you have a value found, it triggers the regions it is a member of to remove the potential values from those too.