TITLE: Probability distribution of $X_N$, $N = min\{n \geq 2: X_n = $ second largest of $ X_1, \ldots, X_n\}$
QUESTION [7 upvotes]: Given a sequence $X_1, X_2, \ldots$ of independent, continuous random variables with the same distribution function $F$ and density function $f$, let $N = min\{n \geq 2: X_n = $ second largest of $ X_1, \ldots, X_n\}$.
Denote $X_N$ as the first random variable which, at the time of observation, is the second largest of those observed so far.  Find the probability density function $f_{X_N}(x)$.
I'm extremely lost on this problem; all I've come up with thus far is to look at random variables $X_M$, the maximum of all observed so far, and $X_{M-1}$, the 'second-to-maximum' but I have been unable to find their respective distribution/density functions.
Thanks in advance for any and all help.

REPLY [2 votes]: A different way to show André Nicolas' result:
Let $Y$ be the second largest $X_i$. Then $Y \le x$ if and only if there is at most one $i$ such that $X_i > x$. Let $F(x) = \mathbb{P}(X_1 \le x)$ be the distribution function of $X_1.$
Using the fact that the $X_i$ are i.i.d.:
$$
\begin{align}
\mathbb{P}(Y \le x) &= \mathbb{P}(\{\exists! i: X_i > x\}) + \mathbb{P}(\{\forall i: X_i \le x\})\\
&= \sum_i \mathbb{P}(X_1 \le x, \dots, X_{i-1} \le x, X_i > x, X_{i+1} \le x, \dots, X_n \le x) + \mathbb{P}(X_1 \le x, \dots, X_n \le x) \\
&= \sum_i \mathbb{P}(X_i > x) \prod_{j \neq i} \mathbb{P}(X_j \le x) + \prod_i \mathbb{P}(X_i \le x) \\
&= \sum_i (1-F(x)) F(x)^{n-1} + F(x)^n \\
&= n (1-F(x)) F(x)^{n-1} + F(x)^n.
\end{align}
$$
Differentiate to get the density $f_Y$.

P.S.: One can also show $\mathbb{P}(Z > x, Y \le x) = \mathbb{P}(Y \le x) - \mathbb{P}(Z \le x)$, even if the $X_i$ are not i.i.d.