Airplane Seat Assignment Probability - Problem
The Airplane Seat Assignment Probability Problem
Imagine you're on an airplane with exactly
๐ซ The first passenger has lost their boarding pass and doesn't know which seat is theirs, so they pick a seat completely at random.
๐ฅ Each subsequent passenger follows this simple rule:
โข If their assigned seat is available, they sit in it
โข If their assigned seat is occupied, they pick any available seat randomly
๐ฏ Your mission: Calculate the probability that the last passenger (passenger n) ends up in their own assigned seat.
This is a classic probability puzzle that appears deceptively complex but has an elegant mathematical solution. The key insight lies in understanding that only certain seats matter in determining the final outcome!
Imagine you're on an airplane with exactly
n seats numbered 1 through n, and n passengers are boarding. Here's where it gets interesting:๐ซ The first passenger has lost their boarding pass and doesn't know which seat is theirs, so they pick a seat completely at random.
๐ฅ Each subsequent passenger follows this simple rule:
โข If their assigned seat is available, they sit in it
โข If their assigned seat is occupied, they pick any available seat randomly
๐ฏ Your mission: Calculate the probability that the last passenger (passenger n) ends up in their own assigned seat.
This is a classic probability puzzle that appears deceptively complex but has an elegant mathematical solution. The key insight lies in understanding that only certain seats matter in determining the final outcome!
Input & Output
example_1.py โ Single Passenger
$
Input:
n = 1
โบ
Output:
1.00000
๐ก Note:
With only one passenger, they must sit in their own seat (seat 1), so the probability is 1.0
example_2.py โ Two Passengers
$
Input:
n = 2
โบ
Output:
0.50000
๐ก Note:
First passenger randomly chooses between seat 1 and seat 2. If they choose seat 1, passenger 2 gets seat 2. If they choose seat 2, passenger 2 gets seat 1. Probability = 1/2
example_3.py โ Large Case
$
Input:
n = 1000
โบ
Output:
0.50000
๐ก Note:
Regardless of the number of passengers, the mathematical principle holds: only seats 1 and n matter, and they are equally likely to be chosen, so the probability remains 1/2
Visualization
Tap to expand
Understanding the Visualization
1
Setup
Hotel has n rooms, first guest has no key
2
Chain Reaction
Each displaced guest continues the random selection
3
Two Outcomes
Process ends when someone picks room 1 or room n
4
Equal Probability
By symmetry, both endings are equally likely
Key Takeaway
๐ฏ Key Insight: The elegant solution emerges from recognizing that this complex-seeming problem reduces to a simple symmetry argument - only two seats matter, and they're equally likely to be chosen!
Time & Space Complexity
Time Complexity
O(1)
The answer is always 1/2 for n > 1, so no computation needed
โ Linear Growth
Space Complexity
O(1)
No additional space required beyond storing the result
โ Linear Space
Constraints
- 1 โค n โค 106
- Output format: Return probability as decimal with exactly 5 decimal places
- For n = 1, answer is 1.00000; for n > 1, answer is 0.50000
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code