Let $X$ and $Y$ be independent random variables, uniformly distributed in the interval $[0, 1]$. Find the CDF and the PDF of $|X - Y|$. This question had already been answered in this post using geometric interpretation, which was quite insightful. What I'd like to grasp is where I made a mistake in my analytical development of this question. It follows as:
\begin{eqnarray}
\\
F_Z(z) &=& P(Z<z)\\
&=& P(|X-Y|<z)\\
&=& P(-z<X-Y<z)\\
&=& P(Y-z<X<z+Y)\\
&=& P(X<z+Y) - P(X<Y-z)\\
&=& F_X(Y+z) - F_X(Y-z)\\
&=& \int_{0}^{1}\!\int_{0}^{y+z}\! f_{X,Y}(x,y) \,\mathrm{d}x{d}y - \int_{0}^{1}\!\int_{0}^{y-z}\! f_{X,Y}(x,y) \,\mathrm{d}x{d}y
\end{eqnarray}
Since $X$ and $Y$ are independent random variables
\begin{eqnarray}
F_Z(z) &=& \int_{0}^{1}\!\int_{0}^{y+z}\! f_{X}(x)f_{Y}(y) \,\mathrm{d}x{d}y - \int_{0}^{1}\!\int_{0}^{y-z}\! f_{X}(x)f_{Y}(y) \,\mathrm{d}x{d}y\\
\end{eqnarray}
From the uniform distribution, we have $f_X(x) = f_Y(y) = 1$, then
\begin{eqnarray}
F_Z(z) &=& \int_{0}^{1}\!\int_{0}^{y+z}\! \,\mathrm{d}x{d}y - \int_{0}^{1}\!\int_{0}^{y-z}\! \,\mathrm{d}x{d}y\\
&=& 2z
\end{eqnarray}
Thank you in advance
 A: As JimB alluded to above, your problem is that you haven't taken into account some issues with the boundaries of your integrals.
If you look at your attempt at the question, you replace $f_X(x)$ with $1$, even for values of $x=y+z$ that are above $1$. So what you should do is replace $y+z$ with min$(y+z,1)$, as this is equivalent to replacing $f_X(x)$ with $0$ at all values of $x$ higher than 1.
Likewise, in the second integral you replace $f_X(x)$ with $1$, even for values of $x=y-z$ below $0$. So what you should do if replace $y-z$ with max$(y-z,0)$, as this is equivalent to replacing $f_X(x)$ with 0 for all values of $x$ lower than 0.
First inner integral then evaluates to min$(y+z,1)$
Second inner integral then evaluates to max$(y-z,0)$
Then as $z$ is fixed, only the values of $y$ decide on which of these is the min/max. So you can split the outer integrals at these points ($y=1-z$ and $y=z$ respectively), and replace the min/max expressions with the corresponding min/max.
$\int_{1-z}^1 1 \; dy + \int_0^{1-z} y+z \; dy$
Here, the first integral corresponds to all the values of $y$ where $y+z>1$, meaning $x$ is guaranteed to be less than $y+z$, so it makes sense that the inner integral evaluated to $1$. The second integral corresponds to the values of $y$ where $y+z\leq 1$, and here the inner integral behaves as you wrote in your attempt.
Likewise, the second integral can also be split into two parts:
$\int_0^z 0 \; dy + \int_z^1 y-z \; dy$
Again, the left integral corresponds to the values of $y$ where $y-z<0$, meaning that $x$ is guaranteed to be greater than $y-z$, so as expected the inner integral evaluated to $0$ (as the condition can never be met). The right integral corresponds to the values of $y$ where $0\leq y-z$, and in this case the inner integral matches your attempt.
Final things to note are that $z>0$ and $1>y>0$ so $y+z>0$ and $y-z$<1 are guaranteed, so those potential boundary problems can be ignored.
Evaluating these integrals and taking the difference as you do in the expression you wrote above, you'll end up with $2z - z^2$ as required.
Hopefully this helps you understand the mistake you made. You can also just put max and min boundaries on the inner integrals and evaluate like that, but I think this way helps to point out where the mistake in your reasoning was, as you can clearly see that at certain points you're hitting the "top" and "bottom" of the CDF.
