FastPrepUCB1 Multi-Armed Bandit Trace

UCB1 Multi-Armed Bandit Trace

Wayfair logoWayfairMediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

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.

  1. Pull every arm once in increasing arm-index order.
  2. For each later step t, where t is the number of pulls already completed, choose the arm maximizing meanReward + sqrt(2 * ln(t) / pullCount).
  3. If scores tie exactly, choose the smaller arm index.
  4. 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 = 4return = [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 = 3return = [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.

More Wayfair problems

See Wayfair hiring insights
public int[] ucb1BanditTrace(int[][] rewardStreams, int rounds) {
    // Write your solution here.
}
rewardStreams[[1,1,1,1],[0,0,0,0]]
rounds4
expected[0,1,0,0]
Checking account…