Airplane Seat Assignment Probability

medium math probability recursion

Problem

n passengers board a plane with exactly n seats. The first passenger has lost their ticket and picks a seat uniformly at random. Every other passenger takes their own assigned seat if it is free; otherwise they too pick a remaining seat at random. Return the probability that the n-th (last) passenger ends up in their own seat n.

Inputn = 2
Output0.50000
Passenger 1 picks seat 1 or seat 2 with equal chance. Only seat-1 leaves seat 2 free for passenger 2, so the probability is 1/2.

def nth_person_gets_nth_seat(n):
    if n == 1:
        return 1.0
    return 0.5
function nthPersonGetsNthSeat(n) {
  if (n === 1) return 1.0;
  return 0.5;
}
class Solution {
    public double nthPersonGetsNthSeat(int n) {
        if (n == 1) return 1.0;
        return 0.5;
    }
}
double nthPersonGetsNthSeat(int n) {
    if (n == 1) return 1.0;
    return 0.5;
}
Time: O(1) Space: O(1)