8

What is the optimal strategy for flipping N pork tenderloins?

The Setup

Today, I decided to pan-sear some pork tenderloin. I sliced the piece into $N=15$ roughly identical cylinders and arranged them on the hot pan. After a couple of minutes, it was time to flip them to sear the other side. This mundane task got me thinking about efficiency: what is the fastest way to turn them all over?

Let's model the problem: We have $N$ items, all in state 0 (uncooked side up). The goal is to get them all to state 1 (cooked side up) with the minimum number of operations.

The available operations are:

  1. Targeted Flip: Use a spatula to flip a single piece, i. This is one operation.
  2. Global Flip: Grab the pan and jerk it upwards, launching all pieces into the air. Each piece has a 50/50 chance of landing in the desired state 1, regardless of its starting state. This is one operation.
  3. Observe: Look at the pan to see which pieces are in which state and get a count, k, of the pieces still in state 0. This is one operation.

Strategy 1 ($S_1$): The Patient Spatula

The most straightforward method is to ignore the pan-toss. I can use my spatula to flip each of the $N$ pieces one by one.

  • Algorithm: For i from 1 to $N$, perform a targeted flip on piece i.
  • Cost: This is deterministic and always takes exactly $N$ operations. For my $N=15$ pieces, this is 15 operations.

Strategy 2 ($S_2$): The One-Pan-Toss

I'm not a professional chef, so my global flip is random. A single pan-toss could be very effective.

  • Algorithm:
    1. Perform one Global Flip (1 op).
    2. Perform an Observe (1 op) to see which k pieces are still unflipped.
    3. Perform k Targeted Flips to clean up the rest.
  • Analysis: The number of remaining unflipped pieces, k, is a random variable. It follows a Binomial Distribution, $k \sim B(N, p=0.5)$. The expected value of k is $E[k] = N \times p = N/2$.
  • Expected Cost: $1 \text{ (global flip)} + 1 \text{ (observe)} + E[k] \text{ (targeted flips)} = \mathbf{2 + N/2}$. For $N=15$, this is $2 + 15/2 = 9.5$ operations on average. This seems much better than 15!

Strategy 3 ($S_3$): The Adaptive Chef (And My Question)

This is where it gets interesting. Strategy 2 commits to cleaning up deterministically after one toss, no matter the outcome. But what if the global flip performs poorly?

Suppose I do the global flip and observe that an unusually high number of pieces, say $k_{bad} > N/2$, are still unflipped. It feels inefficient to resign myself to performing $k_{bad}$ targeted flips. The smart move would be to say, "That was a bad toss, let me try another one."

This leads to an adaptive strategy:

  • Algorithm:
    1. Perform a Global Flip and Observe. Let k be the number of unflipped pieces.
    2. Decision: Compare the cost of the next two possible actions:
      • Go Deterministic: Flip the remaining k pieces. Cost from this point: k.
      • Go Probabilistic: Try another Global Flip. Expected cost from this point: $1 \text{ (flip)} + 1 \text{ (observe)} + N/2 \text{ (cleanup)} = 2+N/2$.
    3. The Rule: If $k \le 2 + N/2$, the deterministic path is cheaper. We stop tossing and clean up with the spatula. Otherwise, we go back to step 1 and try another global flip.

This has to be better than $S_2$, right? Surely, if $k_{bad}$ is sufficiently large, adding another flip improves the amount of operations on average. But the question is:

  1. Is $S_3$ the best way to do it, given these three available operations?
  2. If yes, then, given $N$, what is the expected amount of operations? $E[S_1(N)] = N$ and $E[S_2(N)] = N/2 + 2$, that's simple. But I'm not quite capable of handling the recursive probabilistic equation that appears in $S_3$.
  • 5
    The AI-generated form of the question adds a lot of unnecessary fluff. – Misha Lavrov Jun 26 '25 at 16:08
  • It looks much better after I had it post-process my thoughts though! – Captain Trojan Jun 26 '25 at 16:09
  • I may be wrong, but it seems to me like the distribution of the flipped and unflipped is going to be the same whether you do one global toss or multiple global tosses. If that is true, doing exactly 1 global toss is optimal. So I think S2 is optimal. – Srini Jun 26 '25 at 17:31
  • S1 is optimal only when $N \le 4$, but you already started with $N=15$. So I will still recommend S2. Interested in being proven wrong :) My comments apply for $p=0.5$. If not, there may be an interesting S3 strategy – Srini Jun 26 '25 at 17:35
  • There is a nonzero probability that a global flip results in a clean-up operation that is at least as expensive as five targeted flips... – Toffomat Jun 27 '25 at 07:10

2 Answers2

2

Fun problem. Here is my approach to break down into smaller distributions. I did this for general $N$ (number of tenderloins) and $p$ (probability of global flipping into state $1$).


S2

Suppose you do a global flip and observe to get $k_1 \sim \text{Bin}(N, 1-p)$.

In $S_2$ you have to do $k_1$ more operations (since you cleanup no matter the value of $k_1$). So $S_2(N) = 2 + k_1$, and $E[S_2(N)] = 2 + N(1-p)$.


S3

If $k_1 \leq 2 + \frac{N}{2}$ then $S_3(N) = 2 + k_1$ too. If $k_1 > 2 + \frac{N}{2}$, we do a global flip and observe to get $k_2 \in [0,N]$. If $k_2 \leq 2 + \frac{N}{2}$ then $S_3(N) = 2 + (2 + k_2) = 4 + k_2$. In general, we get $$ S_3 = 2T + k_T \quad \text{when $T$ is the first time where $k_t \leq 2 + \frac{N}{2}$} $$

Consider a sequence of independent binomial variables $(k_t)_{t\in\mathbb{N}}$ where $k_t \sim \text{Bin}(N, 1-p)$ which represents the (hypothetical) sequence of observed after global flips. Note in particular this probability (which could be approximated with a normal distribution for sufficiently large $N$): $$ q = P\left(k_t \leq 2 + \frac{N}{2}\right) = \sum_{n=1}^{\lfloor 2 + \frac{N}{2} \rfloor}{N \choose n}(1-p)^n p^{N-n} $$ This is the probability of stopping the $S_3$ process and going with the deterministic approach. Then consider a geometric distribution $T \sim \text{Geometric}(q)$ that counts the number of global flips until first "success," where $k_T$ is the first one that falls below the threshold to get to the deterministic approach. Note that $E(T) = \frac{1}{q}$, and $k_T$ is a truncated binomial distribution, so $$ E(k_T) = E\left(k_1 \Bigg| k_1 \leq 2 + \frac{N}{2}\right) = \frac{\sum_{n=1}^{\lfloor 2 + \frac{N}{2} \rfloor} n{N \choose n}(1-p)^n p^{N-n}}{\sum_{n=1}^{\lfloor 2 + \frac{N}{2} \rfloor}{N \choose n}(1-p)^n p^{N-n}} $$

Thus we get $$ E[S_3(N)] = 2\left(\frac{1}{q}\right) + E(k_T) $$


S2 vs. S3

There are situations where $S_2$ would be better than $S_3$ (if you get bad global flips, then one flip is better than many). My hypothesis is $S_3$ essentially reduces the variance you would face and thus reduces the expected operations (i.e., if the first flip is bad, then just flip again and it probably won't be twice in a row, if $p \approx 0.5$). I don't have a closed form because it involves multiple convolutions.

However, I have numerical results that can be run in Python:

import pandas as pd
import numpy as np
import scipy as sp

initialize parameters

N = 15 P = 0.99 EXPS = 10**5 SEQUENCE_LENGTH = 100

randomly generate experimental data

ks = np.random.binomial(N, 1-P, size=(EXPS, SEQUENCE_LENGTH))

#########################################################################

Theoretical Analysis

es2 = 2 + N*(1-P)

q = sp.stats.binom(N, 1-P).cdf(2 + N/2)

kt_probs = sp.stats.binom.pmf(np.arange(0, np.floor(es2) + 1, 1), N, 1-P) kt_probs /= kt_probs.sum() ekt = sp.stats.rv_discrete(values = np.array([np.arange(0, np.floor(es2) + 1, 1), kt_probs])).stats()[0]

es3 = 2*(1/q) + ekt

print(f'E(S2) = {es2}') print(f'E(S3) = {es3}')

When N = 15, P = 0.5 we get

E(S2) = 9.5

E(S3) = 9.31573461759632

#########################################################################

Numerical Analysis

global flip once and then flip remaining ones individually

strategy2 = 2 + ks[:, 0]

global flip then check if deterministic then flip again

strategy3 = np.zeros(shape=(EXPS,)) for i in range(EXPS): for t in range(SEQUENCE_LENGTH): # flip and observe strategy3[i] += 2

    # if small number unflipped then go deterministic
    if ks[i, t] <= 2 + es2:
        strategy3[i] += ks[i,t]
        break

numerical expected values

print(f'E(S2) = {strategy2.mean()}') print(f'E(S3) = {strategy3.mean()}')

print(f'P(S3 > S2) = {(strategy2 < strategy3).sum()/EXPS}')

When N = 15, P = 0.5 we get

S2-bar = 9.50989

S3-bar = 9.32748

P-hat(S3 > S2) = 0.02775

I tested for many values of $N$ and $p$ and the results are consistent, but the code could definitely be wrong. As hypothesized, when $p \approx 0.5$, $S_3$ has fewer operations than $S_2$.

Pavan C.
  • 1,727
  • Great work, thank you for the analysis. When I look at the final formula, does this mean that $E[S_3(N)]$ is still in $O(n)$? – Captain Trojan Jun 27 '25 at 00:47
  • 1
    Yes, I think $E(k_T)$ is still $O(N)$, just from the plot. I also observe that as $N$ or $p$ go up, $S_3$ becomes more optimal, and as $N$ or $p$ go down, $S_3$ converges to $S_2$. – Pavan C. Jun 27 '25 at 14:08
0

The classic version of S3 would be to set a threshold on he number that are up after the last toss and toss again if less are up. You compute the expected number of operations as a function of the threshold and minimize it.

Let $T$ be the minimum number of up pieces that you will accept and just flip them one by one after that. Given your threshold the expected number of operations is twice the expected number of flips to reach or exceed your threshold plus the expected number of flips given that you met the threshold. Your S2 is this strategy with a threshold of $0$, so you always accept the first flip.

I find the optimum threshold to be $6$. You get an expected number of up pieces of $8.03$ and it only takes an average of $1.177$ flips to achieve it, giving an expected number of operations of $9.316$ This is not much of an improvement because the cost of a flip is high compared to the improvement you can get.

In the spreadsheet below, the first column is the threshold. The second is the binomial coefficient $15 \choose T$. The third column is the total of all the binomial coefficients on that line and below. It represents the number of cases out of $32768$ that you will accept. the fourth column is $T$ times the binomial coefficient. The fifth column is the sum of entries in the fourth column in that row and below, used in the sixth column to compute the expected number of up pieces with that threshold. The $8.03$ in row $6$ shows you expect that many pieces up with a threshold of $6$. Finally the last column adds twice $32768/$column three, representing the effect of the operations to flip and observe.

enter image description here

Ross Millikan
  • 383,099