Four Colleagues in a Row: Recover the Exact Standing Order
Math & Bit ManipulationAuto-graded · 2 test casesLast 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
- A stands somewhere to the left of C (not necessarily adjacent).
- B is not standing at either end of the row.
- D stands directly next to exactly one of A or C — not both, and not neither.
- 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.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a constraint-satisfaction puzzle over a tiny search space. There are only 4! = 24 ways to arrange four people, so exhaustively checking every permutation against every clue is perfectly efficient. The key insight is applying the most restrictive clues first to prune candidates early, though correctness is guaranteed regardless of order since every clue is verified before any arrangement is accepted.
Deduction walkthrough
Clue 2 immediately limits B to positions 2 or 3 (0-indexed: indices 1 or 2).
Clue 4 ties A's position directly to B's — their indices must differ by exactly 2:
- If B is at index 1, A must be at index 3. But clue 1 then requires C to be further right than index 3, which is impossible. This branch is eliminated.
- If B is at index 2, A must be at index 0 or index 4. Index 4 doesn't exist, so A is at index 0.
That fixes A at index 0 and B at index 2, leaving C and D to fill indices 1 and 3.
Clue 1 is satisfied by both orderings (A is already leftmost, so C anywhere to its right works), so it doesn't separate the two remaining candidates here.
Clue 3 is the tie-breaker. Count how many of {A, C} are immediate neighbors of D:
- If D is at index 3 (C at index 1): D's only neighbor is B at index 2. D touches neither A nor C — count is 0. Eliminated.
- If D is at index 1 (C at index 3): D's neighbors are A at index 0 and B at index 2. D touches A but not C — count is exactly 1. Valid.
The unique solution is A, D, B, C.
Complexity
- Time: O(1) — the search space is fixed at 24 permutations regardless of input.
- Space: O(1) — only a constant number of variables are needed at any point.
Reference solution
from itertools import permutations
from typing import List
def deduce_row_order() -> List[str]:
for order in permutations("ABCD"):
position = {person: i for i, person in enumerate(order)}
# Clue 1: A must be to the left of C
if position["A"] >= position["C"]:
continue
# Clue 2: B cannot occupy either end (index 0 or 3)
if position["B"] in (0, 3):
continue
# Clue 4: exactly one person sits between A and B
if abs(position["A"] - position["B"]) != 2:
continue
# Clue 3: D is adjacent to exactly one of A or C (XOR)
adjacent_to_d = sum(
abs(position["D"] - position[person]) == 1
for person in ("A", "C")
)
if adjacent_to_d == 1:
return list(order)
return []
Common mistakes to avoid
- Reading clue 3 as OR instead of XOR. "Exactly one" means D must border one of {A, C} but not both. Using
adjacent_to_d >= 1would wrongly accept an arrangement where D sits between A and C, touching both. - Confusing "one person between" with "adjacent." Clue 4 says positions differ by 2, not 1. Misreading this as adjacency produces no valid solution.
- Treating "somewhere to the left" as "immediately to the left." Clue 1 only requires A's index to be strictly smaller than C's — any gap is fine.
- Forgetting out-of-bounds positions. When computing
B ± 2, explicitly discard indices outside 0–3. Skipping this step can cause you to miss the clean elimination of the B-at-index-1 case.