Construction of a continuous function which is not bounded on given interval.

You've got the basic idea - you can just generalize it.

Suppose $X\subset \mathbb{R}$ is such that every continuous function on $X$ is bounded. I claim that $X$ is both closed and bounded.

  1. Suppose $c \in \overline{X}\setminus X$, then the function $$ f(x)= \frac{1}{x-c} $$ will be unbounded. Hence, $X$ must be closed.

  2. If $X$ is not bounded, just take $f(x) = x$, then $f$ is unbounded.

Hence, $X$ must be compact.

Conversely, if $X$ is compact, every continuous function is bounded, so just check which of these sets are compact.