For your example, plug in $x = 1$ to get $f(3) = 4$, and plug in $x = -1$ to get $f(3) = -4$. This is a contradiction, since $f(3)$ can only have one value. Hence, no such function $f$ exists.
Now, if $a$ is one-to-one, then $a^{-1}(y)$ is uniquely defined for all $y$ in the range of $a$. Then,
$f(a(x)) = b(x) \leadsto f(a(a^{-1}(y))) = b(a^{-1}(y)) \leadsto f(y) = b(a^{-1}(y))$ for all $y$ in the range of $a$.
So, if $a$ is one-to-one, we can write $f(x) = b(a^{-1}(x))$ on the range of $a$, but we have no information about $f(x)$ for $x$ outside the range of $a$.
For instance, suppose we know that $f(\cos x) = \cos 2x$ for all $x \in \mathbb{R}$. Since $\cos 2x = 2\cos^2 x - 1$ and $\cos x$ has a range of $[-1,1]$, we know that $f(y) = 2y^2-1$ for all $y \in [-1,1]$, but we don't know anything about $f$ outside $[-1,1]$. In fact, we can define $f(y)$ arbitrarily for $|y| > 1$, and $f(\cos x) = \cos 2x$ will still be true for all $x \in \mathbb{R}$.