I recently had this question in an interview:
You are flipping a fair coin until a sequence of four flips repeats itself. The sequences are allowed to overlap. What is the expected number of flips?
For example, if you flip HTHTHT, the sequence HTHT appears in flips 1-4 and 3-6. In this case, we are done after 6 flips. As another example, if you flip HHTTHHTT, the sequence HHTT repeats in flips 1-4 and 5-8, and we are done after 8 flips. What is the expected number of flips?
I've been thinking about this question for a few days now, and I haven't come to an answer. I've tried the simpler problem where we consider a sequence of two flips instead of four flips, but it is still rather difficult. I suspect that there is a nice recursive way to solve this problem, but I can't figure it out.
I am also interested in generalizations of this problem. For example, what is the expected number of flips for a sequence of $n$ to repeat itself? What happens if the coin isn't fair?
I have another interview in a few days, so I would very much like to see how to solve this problem in advance. Any help is appreciated.
Edit:
Based on computational evidence (assuming I didn't make a mistake in the code), it appears that the expected number of flips is about 9.81. I would like to know the exact answer, as well as an analytic solution to this problem.
Edit 2:
Another piece of information that may be of use: I had 30 seconds to answer the question. This makes me believe that there is some "easy" way to get the answer, or they were looking for an approximate answer.
Edit 3:
@r.e.s. has kindly provided exact solutions for $n=1,2,3,4,5$. For $n=6$, numerical computation seems to indicate that the answer is around $18.977$ or $18.978$. I will be updating periodically with approximations for other values of $n$.
Edit 4:
$$\begin{array}{|c|c|}\hlinen & E(L_{n}) \\ \hline1 & \frac{5}{2}\\ \hline2 & \frac{35}{8}\\ \hline3 & \frac{435}{64}\\ \hline4 & \frac{2513}{256}\\ \hline5 & \frac{57922047}{4194304}\\ \hline6 & \approx 18.9775\\ \hline7 & \approx 25.928\\ \hline8 & \approx 35.288\\ \hline\end{array}$$