We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Airplane Seat Assignment Probability

Number: 1362

Difficulty: Medium

Paid? No

Companies: Microstrategy


Problem Description

n passengers board an airplane with exactly n seats. The first passenger has lost the ticket and picks a seat randomly. For every subsequent passenger, they will take their own seat if available, otherwise they pick a seat at random from what remains. Determine the probability that the nth passenger gets to sit in their own seat.


Key Insights

  • The problem has a surprising constant probability for any n >= 2.
  • Instead of simulating the whole process, a pattern (or recurrence) can be observed.
  • The probability turns out to be exactly 1 for n = 1, and 0.5 for any n >= 2.
  • The key insight is that only the fate of the first and the nth seat matters as the randomness propagates.

Space and Time Complexity

Time Complexity: O(1)
Space Complexity: O(1)


Solution

The first passenger picks a random seat. Three outcomes occur:

  1. He picks his own seat: All subsequent passengers, including the nth, will sit in their assigned seats.
  2. He picks the nth passenger's seat: The nth passenger loses his seat.
  3. He picks a seat that is neither his own nor the nth’s. In the third case, the new "problem" reduces to a smaller instance with the same structure. By analyzing the recursive events, it can be shown that the probability is reduced to a 50-50 chance for the nth passenger if n ≥ 2. Thus, if n == 1, the probability is 1.0; otherwise, it is 0.5.

Algorithm:
Check if n is 1. If yes, return 1.0. Otherwise, return 0.5.
Data Structures:
No complex data structures are needed; a simple conditional check works.


Code Solutions

# Function to return the probability that the nth passenger gets their own seat
def nth_seat_probability(n):
    # If only one passenger, they must take their seat.
    if n == 1:
        return 1.0
    # For any n greater than or equal to 2, the probability is 0.5.
    return 0.5

# Example usage:
print(nth_seat_probability(1))  # Output: 1.0
print(nth_seat_probability(2))  # Output: 0.5
← Back to All Questions