Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
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 <= 105Problem summary: 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: Take their own seat if it is still available, and Pick other seats randomly when they find their seat occupied Return the probability that the nth person gets his own seat.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Dynamic Programming
1
2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1227: Airplane Seat Assignment Probability
class Solution {
public double nthPersonGetsNthSeat(int n) {
return n == 1 ? 1 : .5;
}
}
// Accepted solution for LeetCode #1227: Airplane Seat Assignment Probability
func nthPersonGetsNthSeat(n int) float64 {
if n == 1 {
return 1
}
return .5
}
# Accepted solution for LeetCode #1227: Airplane Seat Assignment Probability
class Solution:
def nthPersonGetsNthSeat(self, n: int) -> float:
return 1 if n == 1 else 0.5
// Accepted solution for LeetCode #1227: Airplane Seat Assignment Probability
impl Solution {
pub fn nth_person_gets_nth_seat(n: i32) -> f64 {
return if n == 1 { 1.0 } else { 0.5 };
}
}
// Accepted solution for LeetCode #1227: Airplane Seat Assignment Probability
function nthPersonGetsNthSeat(n: number): number {
return n === 1 ? 1 : 0.5;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.