Problem #101 HARD
The Impossible Crossing — Four Switches, One Bulb
Paradox Cryptography XOR Logic
Problem Statement
A corridor has a single light bulb controlled by four switches — one at each end of a long hallway with two doors in the middle. Each of the four switches can be flipped independently, and flipping any one switch changes the bulb's state (on/off). You are at one end. Without seeing the bulb, and allowed only one walk down the corridor (no return trips), can you always determine the exact state (on or off) that the bulb will be in when you reach the other end? If not, can you guarantee the bulb ends up ON when you arrive?
Answer & Quick Explanation
Think you've got it? Click below to check your answer.
Yes — you can always guarantee the bulb ends up ON. Strategy: as you pass switches 2 and 3, turn them both OFF. At switch 4, flip it ON if S1 (first switch) was OFF; leave it as-is if S1 was ON. Final XOR of all switches = 1, so bulb is ON. Tool: XOR parity.
WOW Moment:
- Four switches. One bulb. You walk one way. You see all four switches as you pass them. You can flip any of them.
- No matter what state the switches start in, you can GUARANTEE the bulb is ON when you arrive.
- The strategy requires only one bit of information: the state of the first switch you see. Everything else can be zeroed out as you walk.
- XOR (exclusive OR) is the mathematics of parity. It appears in: error correction codes, cryptography, RAID storage systems, and network checksums. Every time data is sent reliably across the internet, XOR is silently working in the background.
- The light switch problem is secretly about the mathematics that makes the internet work.
Detailed Editorial Solution
Want to see the step-by-step breakdown? Click below to reveal the editorial.
This problem is an elegant application of parity and Boolean algebra, specifically the XOR (exclusive OR) operation.
Mathematical Representation:
Let the four switches be represented by binary variables S1, S2, S3, S4 ∈ {0, 1}, where 0 represents the switch being down/off, and 1 represents up/on.
The state of the light bulb, B, is determined by the parity of the switches. If an odd number of switches are on, the bulb is ON (1); if an even number are on, the bulb is OFF (0).
Mathematically, this is the XOR sum:
B = S1 ⊕ S2 ⊕ S3 ⊕ S4.
Your constraints:
1. You start at S1. You can see its state and choose whether to flip it.
2. You walk past S2, then S3, and finally reach S4. You can see and flip each switch as you pass, but you can never go backward.
3. You cannot see the light bulb.
The Strategy:
Since you cannot see the initial states of S2, S3, S4 at the start, they represent unknown variables. However, because you can write to them as you walk past, you can overwrite their values to eliminate the uncertainty.
1. When you start at S1, observe its state. Do not flip it. S1 remains in its initial state.
2. Walk to S2. Regardless of its initial state, flip it so that it is OFF (S2' = 0).
3. Walk to S3. Regardless of its initial state, flip it so that it is OFF (S3' = 0).
4. Now you are at S4. The current state of the first three switches is S1, 0, 0.
The XOR sum of the first three switches is:
S1 ⊕ 0 ⊕ 0 = S1.
5. Look at S4. You want the final XOR sum of all four switches to be 1 (bulb ON).
B_final = S1 ⊕ 0 ⊕ 0 ⊕ S4' = 1
S1 ⊕ S4' = 1.
6. This tells you exactly how to set S4':
- If S1 was 1 (ON), you need S4' to be 0 (OFF) so that 1 ⊕ 0 = 1.
- If S1 was 0 (OFF), you need S4' to be 1 (ON) so that 0 ⊕ 1 = 1.
By setting S4' = 1 - S1, you guarantee that the final state of the bulb is ON, regardless of the initial states of all four switches.
To explain the WOW part:
The XOR operation is the foundation of parity-based error detection and correction. In RAID 5 storage, if one hard drive fails, its data is reconstructed by taking the XOR sum of all the remaining drives. In telecommunications, parity bits are added to packets of data to detect if a bit was flipped during transmission. This simple puzzle demonstrates how we can control a complex global state (the bulb) using local operations and parity tracking.