Last Ball

You have 20 blue balls and 14 red balls in a bag. you put your hand in and remove 2 at a time. If they’re of the same color, you add a blue ball to the bag. If they’re of different colors, you add a red ball to the bag. (assume you have a big supply of blue & red balls for this purpose. note: when you take the two balls out, you don’t put them back in, so the number of balls in the bag keeps decreasing). What will be the color of the last ball left in the bag?

Once you tackle that, what if there are 20 blue balls and 13 red balls to start with?

Solution

You always take off Red Balls two by two !

So if you start with 14 Red Balls, you cannot have one single Red ball at the end.

… so the last ball is blue.

But if you start with 13 Red Balls, as you take them off 2 by 2 (at one moment or the other you’ll do it !) you will arrive at a moment when you have 1 red ball in the bag. But as you can only take off Red Balls 2 by 2 (Did I already say that ?!) you’ll remove the last Blue Balls, one by one……

So the lastball will be red…

Oh, by the way, did I tell you that you take off Red Balls 2 by 2 ?! ;->

For tired people here is why you take off Red Balls 2 by 2 :
– If you take off 1 RED and 1 BLUE, in fact you will take off 1 BLUE
– If you take off 2 RED, in fact you will take off 2 RED (and add 1 BLUE)
– If you take off 2 BLUE, in fact you will take off 1 BLUE

2026 Update: Parity Analysis for Interview Problems

Last Ball puzzles (a bag contains colored balls; remove pairs following a rule — what’s the last ball?) are solved by identifying a parity invariant. The color of the final ball is determined entirely by the initial counts modulo 2, not by the sequence of removals.

Classic version: A bag has N red and M blue balls. Each turn: remove 2 balls — if same color, add 1 blue ball; if different colors, add the red ball back. What is the last ball?

Invariant analysis: Red balls: whenever two reds are removed, count decreases by 2; when one red is removed with a blue, red stays the same. So the parity of red balls never changes. If you start with an odd number of red balls, the last ball is red; if even (including 0), the last ball is blue.

def last_ball_color(n_red, n_blue):
    """
    n_red: initial red ball count
    n_blue: initial blue ball count
    Returns the color of the last remaining ball.
    Invariant: parity of red balls is preserved through all operations.
    """
    # Red is odd -> last ball is red
    # Red is even (0, 2, 4...) -> last ball is blue
    return "red" if n_red % 2 == 1 else "blue"

# Verify with simulation
import random

def simulate_last_ball(n_red, n_blue, trials=10_000):
    results = {"red": 0, "blue": 0}
    for _ in range(trials):
        balls = ["red"] * n_red + ["blue"] * n_blue
        while len(balls) > 1:
            random.shuffle(balls)
            b1, b2 = balls.pop(), balls.pop()
            if b1 == b2:
                balls.append("blue")  # Same color -> add blue
            else:
                balls.append("red")   # Different -> add red
        results[balls[0]] += 1
    return results

n_red, n_blue = 3, 4
print(f"Prediction: {last_ball_color(n_red, n_blue)}")
print(f"Simulation: {simulate_last_ball(n_red, n_blue)}")

Why asked in 2026: The parity invariant approach — skip the simulation, find what never changes — is the same reasoning used in algorithm correctness proofs. Candidates who identify invariants immediately signal strong theoretical foundations. Asked at Jane Street, Google, and competitive programming-heavy hiring processes.

Scroll to Top