KatInterview

Truth-Tellers and Liars: Deduce Two Island Identities

Math & Bit ManipulationAuto-graded · 16 test cases
EasyAuto-gradedMath & Bit Manipulationlogiccase-analysismath

Last updated

Tests systematic logical deduction by resolving a two-person truth-teller/liar puzzle through exhaustive case analysis.

Background

On a remote island, every inhabitant belongs to exactly one of two groups: Knights, who always tell the truth, and Knaves, who always lie. You meet two islanders, Perlica and Chen.

Perlica says: "At least one of us is a Knave." Chen says: "Perlica is a Knave."

Your task is to determine the group of each speaker — Knight or Knave — given that both statements must be consistent with the rules.

Task

Write a function that returns a dictionary with three keys:

  • "Perlica" — either "Knight" or "Knave"
  • "Chen" — either "Knight" or "Knave"
  • "reasoning" — a string that explains the derivation: start from the assumption that Perlica is a Knave, show why it leads to a contradiction, conclude that Perlica must be a Knight, then use Perlica's true statement to determine Chen's identity.

The "reasoning" value must match the expected output exactly.

Rules

  • A Knight's statement is always true.
  • A Knave's statement is always false.
  • Every islander is either a Knight or a Knave — no other type exists.

Constraints

  • The function takes no arguments.
  • The return value is a dict with exactly the three string keys described above.
  • There is exactly one consistent assignment; return that unique solution.
  • The "reasoning" string must follow the derivation order specified in the task description.

Examples

Example 1

Input: resolve_two_knights_and_knaves()

Output: {'Perlica': 'Knight', 'Chen': 'Knave', 'reasoning': 'If Perlica were a knave, her statement would be false, meaning neither person is a knave, which is a contradiction. Therefore Perlica is a knight. Her statement is true, so at least one person is a knave. Chen says Perlica is a knave, which is false, so Chen is a knave.'}

Hints

Hint 1

Try assuming Perlica is a Knave and follow the consequences of her statement being false — see if you reach a contradiction.

Hint 2

Once you fix Perlica's identity, evaluate whether Chen's claim about Perlica is true or false, then match that truth value to Chen's group.

Hint 3

Notice that the phrase 'at least one of us' includes the speaker herself — this self-reference is what makes the Knave assumption collapse immediately.