Skip to content

TheoKand/LogicTest

Repository files navigation

The Liar and the Truth Teller

Logic test in C#

I was asked this logic question verbally during a 2nd stage interview for a permanent .NET Developer position with a company in the City of London, in November 2017. The source code implementation in C# is my original work.

There are two guards and two doors. One door leads to freedom, and the other to death. One guard always lies, the other always tells the truth. They know where the two doors go. You do not know which guard is which or which door is which. You may ask one yes or no question to one guard. What do you ask to determine which door leads to freedom?

SHOW SOLUTION

We must ask a question that will result in an answer which is conclusive regardless of the configuration, i.e. if we are asking the liar or the truth teller and if they are guarding the door that leads to freedom or the other door.

SOLUTION

Point to any door and ask the guard in front of it: What will the other guard say if I ask him 'Does your door lead to freedom?'. This question will produce a conclusive answer in all configurations. If you always do the opposite of what the answer says (use the other door) you will always find the right door.

There are four possible configurations, and the answer we get (or it's opposite) must lead to the safe door in all cases:

  1. Ask the truth teller that guards the right door
  2. Ask the liar that guards the right door
  3. Ask the truth teller that guards the wrong door
  4. Ask the liar that guards the wrong door
  • Run the console application for a visual representation of the results. Two questions are evaluated :
    1. Does your door lead to freedom? (no conclusive answer - wrong question)
    2. What will the other guard answer if I ask him 'Does your door lead to freedom?' (conclusive answer - doing the opposite will always lead to freedom)
  • The test suite contains unit tests that check if a question will provide a conclusive answer, and if following this answer (or it's opposite) will lead to freedom, in all possible configurations.

There are more "correct" questions than the one provided here. A correct question will provide a conclusive answer regardless of configuration and the answer (or the opposite) will always lead to freedom.

Releases

No releases published

Packages

No packages published

Languages