KatInterview

Four Colleagues in a Row: Recover the Exact Standing Order

Math & Bit ManipulationAuto-graded · 2 test cases
EasyAuto-gradedMath & Bit Manipulationlogicconstraint-satisfactionbrute-force

Last updated

Tests constraint satisfaction and logical deduction by finding the unique left-to-right arrangement of four people from four positional clues.

Four Colleagues in a Row

Four colleagues — A, B, C, and D — are lined up in a single row, numbered from left (position 1) to right (position 4). Exactly one arrangement satisfies all four clues below. Your job is to find it.

Clues

  1. A stands somewhere to the left of C (not necessarily adjacent).
  2. B is not standing at either end of the row.
  3. D stands directly next to exactly one of A or C — not both, and not neither.
  4. There is exactly one person standing between A and B.

Return the unique arrangement as a list of four single-character strings, ordered from leftmost to rightmost.


Constraints

  • The four people are always "A", "B", "C", and "D".
  • Every person occupies a distinct position (1 through 4); no two people share a spot.
  • "Somewhere to the left" in clue 1 means any strictly smaller position number, not necessarily adjacent.
  • "Directly next to" in clue 3 means the two positions differ by exactly 1.
  • "Exactly one person between A and B" in clue 4 means their positions differ by exactly 2.
  • Clue 3 uses exclusive counting: D must be adjacent to one of {A, C} but not both at the same time.
  • There is guaranteed to be exactly one valid arrangement; return it as a list of four single-character strings.

Examples

Example 1

Input: deduce_row_order()

Output: ['A', 'D', 'B', 'C']

Hints

Hint 1

Start with the clues that cut the most options at once. Clue 2 limits B to only two possible positions, and clue 4 then tightly constrains where A can go — work out all (B, A) pairs before touching the other clues.

Hint 2

Once you have fixed candidate positions for A and B, only two slots remain for C and D. Check clue 1 to see whether both orderings of C and D survive or only one.

Hint 3

Apply clue 3 last as a tie-breaker: for each remaining candidate, count how many of {A, C} are immediate neighbors of D. You need that count to equal exactly 1 — think XOR, not OR.