AceTheInterview
Jobs in Pune | Work better in teams | Socialize with friends | Submit Q&A | Tell a friend
Search site for 

Top 100 Interview Questions & Answers in a convenient and easy to read book!

“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”

– Ravi, California

Read more comments...

Interview Questions And Answers RSS Feed

Answers »

  1. Submitted By: Vida — January 12, 2007
    +19 votes
      + -

    SUDOKU falls into the category of NP problems, i.e it grows exponentially. It is one of the unsolved problems. There is no known solution for SUDOKU .. however, if the size is constrained, you can write an algorithm.

  2. Submitted By: Hima — March 21, 2007
    -1 votes
      + -

    NP problem only means that there is no known exponential solution. Does not mean there is no solution.

  3. Submitted By: Jay — March 28, 2007
    -1 votes
      + -

    you can use dynamic programming to solve Sudoku.
    I can imagine a simple greedy algorithm.

  4. Submitted By: Ben Dover — April 11, 2007
    -6 votes
      + -

    And if you fix the size of the sudoku to a constant, the algorithm actually runs in O(1) time.

  5. Submitted By: krishna — May 16, 2007
    -6 votes
      + -

    One possible soln could be:

    Generate all nxn cominations of sudoku tables. Store them . This is one time cost.

    When the input is given , try to map the input nxn square to already generated solutions.

  6. Submitted By: Will — May 24, 2007
    +6 votes
      + -

    @krishna: Have you ever decided to try this? It’s a n*n^(n*n)^2) operation to find all the legitimate answers. We’re talking several lifetimes of computational power on a normal machine. I just wrote a PERL program (not the fastest I understand) with recursion… try it. You’re better off writing a set of functions to solve each puzzle individually.

  7. Submitted By: Alex — August 14, 2007
    +8 votes
      + -

    There are some basic mistakes above that I want to clear up.

    >SUDOKU falls into the category of NP problems, i.e it grows exponentially.

    What you should be saying is that Sudoku is NP-Complete. NP contains all sorts of trivial problems. The NP-Complete problems are the ones which researchers suspect to be computationally intractable.

    >NP problem only means that there is no known exponential solution. Does not mean there is no solution.

    This statement is false on many levels. Firstly, there are plenty of problems in NP which have deterministic polytime solutions. But even if we assume you were talking about NP-Complete problems, the statement that “NP-Complete problem only means that there is no known exponential solution.” is still false. All NP-Complete problems have exponential algorithms. What you probably meant to say is that NP-Complete problems have no known polynomial time algorithms.

    Incidentally, if you want my 2 cent answer for the question, probably the easiest way to solve Sudoku would be to encode it as a SAT instance and feed it to a clause learning algorithm.

    This is probably a good solution because

    a) Programing a reduction is much easier than programming a hard-core algorithm. This will save time and it will have fewer bugs in it.

    b) You get a fully debugged SAT-solving algorithm for free, which saves you lots of time and development money.

    and c) The SAT solver will probably be faster than any program you could write anyways.

  8. Submitted By: Itay — August 29, 2007
    +2 votes
      + -

    I agree with Alex (9).
    But just in case we want to do this algorithm, then the answer will be the one below.

    Because this is problem is NP complete, the only solution is to go over all the possible answers until a correct answer is found.

    For each column 1 until column 9 {
    For each cell 1 until 9 {
    for i=1 to 9 {
    check(i); // check that i doesnt exists already in this row, column or square.
    if (i is legal) continue to the next cell in the column.
    }
    once finishing filling the column go to the next column.
    if at some point the filling failed (i.e. the column couldnt find any legal values) then go back and try the next i.

    This is a bit of a blur, but if you do it recuresively than it gets clearer and it is not that hard.

  9. Submitted By: Putzig — August 30, 2007
    +1 votes
      + -

    Good job Itay.

    But before you start fill in all the squares that only have one possible choice.

    Then as you proceed always choose the most constrained squares. If you chose the spot that has only two possibilities and you choose the wrong one it will have an error faster and you will prune larger portions of the tree.

    I don’t understand the argument over if this can be solved. I’ve seen solvers for sodoku that finish the puzzle almost instantly. NP completeness is not too important when your search space is so small like in sodoku. When is the last time you heard of an 11 by 11 sodoku puzzle?

  10. Submitted By: Rodrigo Gomez Avila — October 4, 2007
    not yet rated
      + -

    Sudoku is indeed NP-Complete for puzzles of size n. But probably you should ask to the interviewer if he means the general (n-sized) or the common (3-sized) (the one you can find in magazines) case. If he means the latter (which he probably does) the better solution is backtracking.

  11. Submitted By: Bryan — October 6, 2007
    not yet rated
      + -

    First input or get all fixed values:
    Then test each box such that for (A,1-3), if (A,1-9 == n, (A-I,1) == n, (B,1-3) == n or (C,1-3) == n then A1 != n, and so on for A1-I9, replacing ABC with DEF for rows D, E and F, and with GHI for rows G, H, and I. Continue until solved.

  12. Submitted By: Tom Robinson — October 29, 2007
    +1 votes
      + -

    No, no, NO!

    The idea of \”NP\” is probably one of the most misunderstood concepts in computer science. It does NOT mean solutions are \”not polynomial\” or exponential or anything like that. It stands for Non-deterministic Polynomial time (meaning it could be solved in polynomial time on a non-deterministic Turing machine… which doesn\’t exist)

    Roughly speaking, this means: given a proposed solution, it is easy (in polynomial time) to verify whether or not that solution is correct.

    What most people mean when they say something is \”NP\” is actually that it is \”NP complete\”, which means the problem is just as difficult as thousands of other NP complete problems, of which there has never been found a polynomial time solution (and if a polynomial time solution WERE found for ANY NP problem, then ALL NP problems could be solved in polynomial time)

    Sudoku is NP-complete, so there\’s no known polynomial time solution, and it seems unlikely one will ever be found, but it hasn\’t been proven.

    That said, normal 9×9 Sudoku isn\’t very big, and could be easily solved using a number of approaches.

  13. Submitted By: Tom Robinson — October 29, 2007
    +0 votes
      + -

    Prolog (or similar logic systems) would be a neat way to solve Sudoku.

  14. Submitted By: MAYANK CHHAJED — May 20, 2008
    not yet rated
      + -

    sudoku one of the famous problems can easily be solved using Donald Knuth’s Dancing Link Algorithm..

  15. Leave an Answer/Comment

    To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
    Click to hear an audio file of the anti-spam word

Our Sponsors
Our Sponsors
Contact Us | FAQ | Sitemap | Terms of Use | Privacy Policy | Tell a Friend

Copyright © 1999-2006 Jeeve Technologies LLC. All rights reserved.