Airplane Seat Assignment Probability

n passengers board an airplane with exactly n seats. The first passenger has lost the ticket and picks a seat randomly. After that, the rest of the passengers will:

  • Take their own seat if it is still available.
  • Pick another seat randomly when they find their seat occupied.

Return the probability that the n^th person gets his own seat.

Example 1
Inputn = 1
Output1
The first person can only get the first seat.
Example 2
Inputn = 2
Output0.5
The second person has a probability of 0.5 to get the second seat, which happens when the first person gets the first seat.

Constraints

  • 1 <= n <= 10^5

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate