UCB1 Multi-Armed Bandit Trace
Reported by candidates from Wayfair's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Wayfair reported this one in September 2026, and it's less a puzzle than a bookkeeping test. You simulate UCB1 for up to 100000 rounds across at most 100 arms, and the whole thing hinges on tracking per-arm sums, pull counts, and a pointer into each reward stream. If you've got an OA invite for this, expect to write a clean loop and not much else. The risk is floating point and tie handling, not the algorithm. StealthCoder sits invisible on your screen as a safety net if you blank mid-assessment, but the shape of this problem is simple once you see it.
The problem
Simulate the deterministic action choices of the UCB1 multi-armed-bandit strategy. rewardStreams[a] contains the rewards observed from arm a, in the order that arm is pulled. Pull every arm once in increasing arm-index order. For each later step t, where t is the number of pulls already completed, choose the arm maximizing meanReward + sqrt(2 * ln(t) / pullCount). If scores tie exactly, choose the smaller arm index. Consume the next unused reward from the chosen arm and update its statistics. Return the chosen arm index for each of the first rounds pulls. Inputs guarantee that every chosen arm has another reward available. Function ucb1BanditTrace(rewardStreams: int[][], rounds: int) → int[] Examples Example 1 rewardStreams = [[1,1,1,1],[0,0,0,0]] rounds = 4 return = [0,1,0,0] After the mandatory exploration pulls, arm 0's empirical reward advantage keeps its UCB score ahead for both remaining rounds. Example 2 rewardStreams = [[0,1,1],[0,1,1]] rounds = 3 return = [0,1,0] The scores tie after one zero reward from each arm, so arm 0 wins the deterministic tie. Constraints 1 <= rewardStreams.length <= 100. rewardStreams.length <= rounds <= 100000. Rewards are integers in [0,1000000]. Every arm stream is long enough for the pulls selected by UCB1.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The data structure is three parallel arrays of size n: reward sum, pull count, and next-reward index. Pull each arm once in order, then for each step t compute score = sum/count + sqrt(2*ln(t)/count) for every arm and take the max. Use strict greater-than while scanning arms in increasing index, so ties automatically go to the smaller index. Total work is rounds times arms, about 10 million operations, which is fine. The pitfall is t. It's the number of pulls already completed, so the first scored step uses t equal to the arm count, not zero. Don't use ln(0). Compute the mean as a double, not integer division. Example 2 shows exact ties, so don't add epsilon tricks that break the tie rule. If you freeze on the formula, StealthCoder can give you the loop as a live hedge.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill UCB1 Multi-Armed Bandit Trace cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Wayfair's OA.
Wayfair reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
UCB1 Multi-Armed Bandit Trace FAQ
What's the trick in the Wayfair UCB1 bandit problem?+
There isn't a clever trick. It's a direct simulation. Keep sum, count, and a stream pointer per arm, then rescan all arms each round for the max UCB score. The only care points are the right value of t and tie-breaking by smaller index.
How do I handle ties correctly?+
Scan arms from index 0 upward and only replace the current best when the new score is strictly greater. That gives the smaller index on exact ties automatically. Example 2 tests this directly, where both arms have identical stats after the first pulls.
What should t be in the formula?+
t is the number of pulls already completed before the current choice. After pulling every arm once, the first scored step has t equal to the number of arms. Using the round index off by one will give wrong answers on close scores.
Will floating point break my answer?+
Use doubles throughout and compute the mean as sum divided by count in floating point, never integer division. Compare scores with plain greater-than, no epsilon. Exact ties produce identical computations for identical stats, so the tie rule still works as intended.
How do I prepare for this in 48 hours?+
Write the simulation once from scratch with the three arrays and test it against both examples. Then check the complexity: rounds times arms is at most 10 million, so no heap is needed. Spend the rest of your time on edge cases like a single arm.