By | December 12, 2019

Zach Wissner-Gross’ column “The Riddler” over at FiveThirtyEight poses the following interesting question, attributed originally to Austin Chen:

You have a playlist with exactly 100 tracks (i.e., songs), numbered 1 to 100. To go to another track, there are two buttons you can press: (1) “Next”, which will take you to the next track in the list or back to song 1 if you are currently on track 100, and (2) “Random”, which will take you to a track chosen uniformly from among the 100 tracks. Pressing “Random” can restart the track you’re already listening to — this will happen 1 percent of the time you press the “Random” button.

For example, if you started on track 73, and you pressed the buttons in the sequence “Random, Next, Random, Random, Next, Next, Random, Next,” you might get the following sequence of track numbers: 73, 30, 31, 67, 12, 13, 14, 89, 90. You always know the number of the track you’re currently listening to.

Your goal is to get to your favorite song (on track 42, of course) with as few button presses as possible. What should your general strategy be? Assuming you start on a random track, what is the average number of button presses you would need to make to reach your favorite song?

It is easy to see that no sensible strategy will interleave calls to “Random” and “Next”: any pair of calls “Next, Random” can be made into a shorter call “Random”. Thus, our strategy will be to first make all the “Random” calls until we fall into a “neighborhood” of numbers ${t, t+1, \dots, 42}$, where $t \ge 1$ is a threshold parameter, and then hit “Next” until we arrive at the desired 42. The question is the, what is the optimal value of $t$?

### Reindexing the problem

Let us first remove the awkwardness in the problem in the arbitrary choice of the desired track to be $42$ and the fact that hitting “Next” 100 times on $100$ takes us back to $1$.

More specifically, we will set the desired track to be $1$ and assume that we have access to the operations “Random” and “Previous” (instead of “Next”), where calling “Previous” on any track $x > 1$ takes us to track $x-1$ (we will never need to call “Previous” on the desired track $1$, so the behavior is left undefined, but could easily be defined as taking us to track $100$).

### The strategy

Suppose we have defined our threshold track $t > 1$. Recalling that the problem statement says that we start at a random track, our strategy is:

1. Assume the first track $X_0 \sim \mathsf{Uniform}(1,\dots,100)$.
2. Let $n = 0$.
3. While $(t < X_n)$:
1. Set $n = n + 1$.
2. Sample $X_n \sim \mathsf{Uniform}(1,\dots,100)$ by calling “Random”.
4. Hit “Previous” exactly $X_n-1$ times to arrive at track $1$.

### Computing the expected number of operations

Now let us compute how many “Random” and “Previous” operations our strategy needs on average. We can define the number $N_t$ of operations for any threshold $t$ as a random variable $$N_t = n\mathbf{I}[t < X_0] + (X_n-1),$$ where the random variable $n$ is the random number of iterations of the While loop. The indicator function $\mathbf{I}[.]$ is 1 when its argument is true and zero otherwise, which ensures that the contribution of the While loop is zero if the loop is never entered in the first place (i.e., when $X_0 < t$).

Applying expectations, we obtain: $$\mathbb{E}[N_t] = \mathbb{E}[n\mathbf{I}[t < X_0] + (X_n-1)]\ = \mathbb{E}[n]\mathbb{E}[\mathbf{I}[t < X_0]] + \mathbb{E}[X_n]-1,$$ where we have used linearity of expectations (twice) and the property $\mathbb{E}[XY] = \mathbb{E}[X]\mathbb{E}[Y]$ when $X$ and $Y$ are independent (the number of iterations of the While loop—given that it executes—is independent of the initial track $X_0$).

Now let us analyze the expectations:

1. $\mathbb{E}[n] = 100/t$, since $n$ is a Geometric random variable with success probability $t/100$

2. $\mathbb{E}[\mathbf{I}[t < X_0]] = \mathbb{P}[t < X_0] = (100-t)/100$ since $X_0$ is uniform over $\lbrace 1,\dots,100 \rbrace$.

3. $\mathbb{E}[X_n] = \mathbb{E}[\mathbb{E}[X_n | n]] = (1+t)/2$, since $X_n$ is uniform over $\lbrace 1, \dots, t \rbrace$, independently of the value of $n$.

Substituting these terms $$\mathbb{E}[N_t] = \frac{100}{t}\frac{100-t}{100} + \frac{1+t}{2}-1.$$

### Minimizing the expected number of operations

Having derived the expected number $N_t$ of operations for any threshold $t$, let us ask Wolfram Alpha to plot and minimize $\mathbb{E}[{N_t}]$. The output is shown below.

The optimum $t = 10\sqrt(2) \approx 14.142$. The corresponding integer threshold is $t=14$ with $\mathbb{E}[N_{14}] = 177/14 \approx 12.6$ operations.

### Final thoughts

Well, there is my 2c on this interesting problem (laid out in more detail than 2c, though).

The expression $\mathbb{E}[N_t]$ can be simplified to $100/t + t/2 – 3/2$ and interpreted from there. The first term is the expected number of iterations of the While loop (“Random” operations) and the second term is the expected number of “Previous” operations. The third term is a correction factor which accounts for the fact that the While loop has the possibility of executing for zero iterations since we start at the initial track for “free”, i.e., without a “Random” operation, which might already be in the desired “neighborhood”.

The proposed strategy can be adapted to find the appropriate threshold for any desired track (e.g., 42, as in the original problem). Does anyone have alternative strategies that might deliver a lower number of operations?