The Pass the Pillow problem simulates n people in a line passing a pillow. The pillow starts at position 1, moves right until reaching position n, then reverses and moves left, repeating. Given the time t, find who holds the pillow. This easy coding problem tests mathematical modular arithmetic to avoid simulation. The math and simulation interview pattern is demonstrated.
Microsoft, Meta, Amazon, Google, and Bloomberg ask this as a quick math reasoning test. Instead of simulating step by step (O(t)), the optimal solution computes the answer in O(1) using the period and direction. It tests whether candidates can recognize periodic patterns.
Period computation. The pillow completes a full cycle (1→n→1) in 2*(n-1) steps. Compute pos = t % (2*(n-1)). If pos == 0, pos = 2*(n-1). If pos <= n-1: pillow is at position 1 + pos. Else: pillow is at position 2*n - 1 - pos.
n=4, t=5. Period=6. pos=5%6=5. Since 5>n-1=3: position=2*4-1-5=2. Answer = 2.
n=4, t=10. pos=10%6=4. Since 4>3: position=8-1-4=3. Answer = 3.
n=3, t=2. pos=2. 2 ≤ 2=n-1: position=1+2=3. Answer = 3.
Pillow/bouncing problems with periodic behavior always compute the answer modulo the period. Recognize the structure: period = 2*(n-1) for a line of n people. After computing position within the period, determine direction (left-going vs right-going). This pattern applies to "bouncing ball in a room," "oscillating pointer," and similar cyclic motion problems. O(1) solution always preferred over simulation.