n passengers board an airplane with exactly n seats. The first passenger has lost the ticket and picks a seat randomly. But after that, the rest of the passengers will:
Return the probability that the nth person gets his own seat.
Example 1:
Input: n = 1 Output: 1.00000 Explanation: The first person can only get the first seat.
Example 2:
Input: n = 2 Output: 0.50000 Explanation: The second person has a probability of 0.5 to get the second seat (when first person gets the first seat).
Constraints:
1 <= n <= 105When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To solve this problem with brute force, we will simulate every single seating arrangement that can occur. Then, for each arrangement, we can check if the last person sits in their assigned seat. Finally, we count the number of times the last person sits in their seat and divide by the total number of possible seating arrangements to get the probability.
Here's how the algorithm would work step-by-step:
def airplane_seat_assignment_probability(number_of_seats):
total_arrangements = 0
last_person_correct = 0
def simulate_seating(seats_available, current_passenger):
nonlocal total_arrangements, last_person_correct
# Base case: all passengers are seated
if current_passenger > number_of_seats:
total_arrangements += 1
if seats_available[number_of_seats - 1] == 1:
last_person_correct += 1
return
# Simulate first passenger's choice
if current_passenger == 1:
for seat_choice in range(number_of_seats):
seats_copy = seats_available[:]
seats_copy[seat_choice] = 1
simulate_seating(seats_copy, current_passenger + 1)
# For other passengers:
else:
# If seat is available, passenger sits in assigned seat
if seats_available[current_passenger - 1] == 0:
seats_copy = seats_available[:]
seats_copy[current_passenger - 1] = 1
simulate_seating(seats_copy, current_passenger + 1)
# If seat is taken, passenger chooses a random seat
else:
available_seats_indices = [i for i, seat in enumerate(seats_available) if seat == 0]
for seat_choice_index in range(len(available_seats_indices)):
seats_copy = seats_available[:]
chosen_seat_index = available_seats_indices[seat_choice_index]
seats_copy[chosen_seat_index] = 1
simulate_seating(seats_copy, current_passenger + 1)
# Initialize available seats
available_seats = [0] * number_of_seats
# Start simulation
simulate_seating(available_seats, 1)
# Calculate and return probability
return last_person_correct / total_arrangementsThis problem seems complicated, but there's a surprising shortcut. Instead of tracking every passenger's path, we realize most of the time, things go smoothly. We only care about the situations where things go wrong.
Here's how the algorithm would work step-by-step:
def airplane_seat_assignment_probability(number_of_passengers):
if number_of_passengers <= 1:
return 1.0
# If first passenger picks their own seat,
# last passenger will get theirs.
# If first passenger picks last passenger's seat,
# last passenger won't get theirs.
# Because of the probabilistic 'chain reaction',
# the last passenger has a 50% chance.
return 0.5| Case | How to Handle |
|---|---|
| n = 1 (only one passenger) | The first passenger will always take their own seat, thus the last passenger has 0 probability of sitting in their own seat, which means the probability of sitting in the assigned seat is 0 in the problem, but the problem only cares the probability of last passenger sitting in their assigned seat, which is 1 for n = 1. |
| n = 2 (two passengers) | The first passenger either takes their own seat (probability 1/2, last passenger gets own seat) or the second passenger's seat (probability 1/2, last passenger doesn't get own seat), resulting in a 50% chance. |
| Large n (scalability) | The probability converges to 0.5 regardless of the size of n for n > 1; thus, the solution will be O(1) in time and space, and scales perfectly. |
| Zero or negative input for n | Return 0 as there are no passengers or seats in such scenarios, indicating an invalid input. |
| Integer overflow for very large n | The problem doesn't require storing intermediate results depending on n, and the problem definition doesn't include dealing with extremely large integer, thus we can ignore the integer overflow situation. |
| Floating-point precision with extremely large n (although it is constant) | The resulting value of probability is either 0 or 0.5 or 1, so floating-point precision is not a significant concern. |
| n close to the upper limit of integer representation | Although the problem states to only calculate based on 'n', the calculation simplifies and does not require manipulation of 'n', therefore the integer limit is unlikely to be reached and is not a main issue. |
| The last passenger's assigned seat is already taken by a previous passenger | The process continues by randomly assigning seats until either the last passenger finds their seat or all seats are taken, and such intermediate cases are handled correctly by the recursive or iterative structure of the solution which ultimately converges to probability of 0.5 for n > 1 and 1 for n = 1. |