Wavefunction as a combination of two stationary states - how to find those states? Lets say we have a particle in a infinite square well which has a wavefunction like this ($A$ is some constant and $d$ is the width of the well):
\begin{align}
A\left[ \sin \left(\frac{2 \pi x}{d}\right) + \frac{1}{3}\sin \left(\frac{3 \pi x}{d}\right)\right]
\end{align}
How do we know from which stationary states this wavefunction is composed of? I would say that it is a combination of 1st and 2nd excited state, but the anwser in the book is 1. and 3. excited state. How do we find this out?
 A: For 1D Schrödinger equation, $n$th stationary state wavefunction has exactly $n-1$ zeroes. Thus, $k$th excited state wavefunction has exactly $k$ zeroes. Now if you look at your wavefunction, it's composed from two stationary states:

Count their zeroes (don't take borders into account — they are because of infinite height of well). You'll see that you're right, these states are 1st and 2nd excited states. Your book most likely has a typo.
In more general case, as asked in comments, you have to deal with your wavefunction by definition of superposition of states. Let your function $\psi$ be as follows:
$$\psi=
A\left[ \cos \left(\frac{7 \pi x}{d}\right) + \frac{1}{3}\sin \left(\frac{4 \pi x}{d}\right)\right]$$
And your eigenfunctions are:
$$\phi_k=B_k\sin\left(\frac{k\pi x}d\right)$$
To find how much of $\phi_k$ is in your $\psi$, you find scalar product:
$$\left<\psi|\phi_k\right>=\int_V \psi^*\phi_k dx=\int_0^d AB_k \sin\left(\frac{k\pi x}d\right)\left[\cos\left(\frac{7\pi x}d\right)+\frac13\sin\left(\frac{4\pi x}d\right)\right]dx$$
As $\cos(x)$ is not orthogonal to any $\sin(ax)$ on $[0,d]$, you will have infinitely many non-zero $\left<\psi|\phi_k\right>$:

For comparison let's see what this method gives for original question where there's just a superposition of states 2 & 3:

Here only these 2nd and 3rd states are non-zero.
A: If you want to do this analytically, you need to know what the stationary states of the system are. Once you have them, you can use the fact that the stationary states are both orthonormal (the inner product of any two stationary states $i$ and $j$ is $\delta_{ij}$) and complete (any state can be expressed as a sum of stationary states).
Consider an arbitrary state $\psi$. Because the stationary states are complete, $\psi$ can be written as $$\psi =\sum_i c_i \phi_i $$
where $\phi_i$ are the stationary states, and $c_i$ are constants. To find the $c_i$ for a given $\phi$, we can use the orthogonality relation $$\langle\phi_i|\phi_j\rangle \equiv\int\phi_i^*(x)\phi_j(x)dx  = \delta_{ij} $$
With a little bit of algebra, we eventually arrive at
$$c_i=\langle\phi_i|\psi\rangle =\int\phi_i^*(x)\psi(x) dx $$
The state $\psi$ is then composed of all the $\phi_i$ for which the corresponding $c_i$ is not zero.
