Knights, Knaves and Their Statements
Updated
Problem statement
There are n people, numbered 0 to n - 1. Each person is either a knight, who always tells the truth, or a knave, who always lies.
Person i makes exactly one statement, statements[i], in one of these forms (j and k may be any people, including the speaker):
"KNIGHT j": personjis a knight."KNAVE j": personjis a knave."SAME j k": personsjandkare the same type."DIFFERENT j k": personsjandkare different types.
If exactly one assignment of types is consistent with all statements, return it as a string of length n whose i-th character is 'K' (knight) or 'N' (knave). Return "NONE" if no assignment is consistent and "MANY" if more than one is.
Function
knightsAndKnaves(statements: String[]) → StringExamples
Example 1
statements = ["DIFFERENT 0 1","KNAVE 0"]return = "KN"This is the interview case. Person 0 says the two of them are of different types, and person 1 says person 0 is a knave.
If 0 is a knight, the two differ, so 1 is a knave, and 1's claim that 0 is a knave is false, as a knave's claim should be. If 0 were a knave, they would be the same type, so 1 would be a knave too, but then 1's claim would be true. So the answer is "KN".
Example 2
statements = ["KNIGHT 0","KNIGHT 1"]return = "MANY"Each person says they are a knight, which every person can say. All four assignments fit, so the answer is "MANY".
Example 3
statements = ["KNAVE 0","KNIGHT 0"]return = "NONE"Person 0 says "I am a knave". A knight cannot say it (it would be false) and a knave cannot say it (it would be true). No assignment fits: "NONE".
Constraints
2 <= statements.length <= 20- Each statement is
"KNIGHT j","KNAVE j","SAME j k"or"DIFFERENT j k"with single spaces, where0 <= j, k < statements.lengthare written without leading zeros.
More Susquehanna International Group (SIG) Online Assessment questions
- Catch Fish with Reusable BaitsOA · Seen Nov 2026
- Bold Play to Reach a GoalOA · Seen Nov 2026
- Cats, Truth and Who Has MoreOA · Seen Nov 2026
- Counting Cows by Their LegsOA · Seen Nov 2026
- Enough Muffins for the LineOA · Seen Nov 2026
- Expected Consecutive Pairs Among Chosen NumbersOA · Seen Nov 2026
- Four-by-Four Boundary GridOA · Seen Nov 2026
- Frog Paths Without Long RunsOA · Seen Nov 2026
- Higher Roll, or Lose the SumOA · Seen Nov 2026
- Is a Popular Story Fake?OA · Seen Nov 2026
Similar questions at other companies
- Choose the Next Minesweeper ActionRunloop · PHONE SCREEN
- Count Subarrays with a Perfect-Square ProductMicrosoft · OAMicrosoft interview questions
- Repeated Mirror XORMathWorks · OAMathWorks interview questions
- Minimum Power-of-Two Removal OperationsIBM · OAIBM interview questions
- Next Number with Disjoint DigitsElevenLabs · OAElevenLabs interview questions
- Minimum Power-of-Two Removal OperationsVisa · OAVisa interview questions
- Beautiful Number OrderingsDocuSign · OADocuSign interview questions
- Minimum Operations To Reduce To ZeroUber · OAUber interview questions
- City Infection NumberArcesium · OAArcesium interview questions
- Minimum Power-of-Two AdjustmentsNVIDIA · OANVIDIA interview questions