$\mathbf{Algorithm}$ $\ThreeHeadsOrThreeTails(k)$:
$\quad \text{flip a fair coin three times};$
$\quad \mathbf{if}\ \text{the result is $HHH$ or $TTT$}$
$\quad \quad \mathbf{then}\ \mathrm{return}\ k$
$\quad \mathbf{else}$
$\quad \quad \ThreeHeadsOrThreeTails(k + 1)$
$\quad \mathbf{endif}$
So um, this algorithm pretty much counts the number of attempts we make until we get $HHH$ or $TTT$
We have a $ \frac{1}{8} $ chance of flipping an $HHH$
We have a $ \frac{1}{8} $ chance of flipping an $TTT$
This gives us a $ \frac{1}{4} $ chance of flipping either $HHH$ or $TTT$
By the geometric distribution, the expected value of the number of attempts until we get a success is $ \frac{1}{p} $
Therefore, the expected value of $X$ is $ \frac{1}{ \frac{1}{4}} = 4 $