This is exercise $19$ from fourth chapter of baby Rudin.
Suppose $f$ is a real function with domain $R^1$ which has IVP i.e. if $f(a) < c < f(b)$, then $f(x)=c$ for some $x$ between $a$ and $b$. Suppose also for every rational $r$, that the set of all $x$ with $f(x)=r$ is closed. Prove that $f$ is continuous.
I use the hint given by author and got the answer. You can look here.
When I was thinking about this problem, I drew some graphs. One of the graph I draw has puzzled me. It seems to satisfy both the hypotheses, but fails to be continuous.
The graph is as follows.

Graph seems to satisfy IVP and also any horizontal line $y=k$ will hit graph at most twice, so second hypotheses also seems to satisfy. But $f$ is not continuous at origin. What I am messing up here. Thanks.