Elastic collision and spring 
Bodies $A$ and $B$ are moving in the same direction in a straight line with a constant velocities on a frictionless surface. The mass and the velocity of $A$ are $2 \text{kg}$ and $10 \text{m/s}$. The mass and the velocity of $B$ are $6 \text{kg}$ and $4 \text{m/s}$. A spring is connected to the back of the body $B$ and its rate is $800 \text{N/m}$.
  
  
*
  
*What is the velocity of $A$ relatively to $B$ before the collision and after the collision?
(Answer: $6 \text{m/s}$ and $-6 \text{m/s}$)
  
*What is the relative velocity between the objects when the spring achieves its maximum contraction? 
(Answer: $0$)


I had no problem with the first question. We know that in elastic collision the coefficient of restitution is $1$, therefore the relative velocities before and after the collision are equal but are opposite in sign. The relatve velocity before the collision is $10-4=6\text{m/s}$ .
My trouble is with the second question. I though a lot about it and came to a contradiction in my thoughts (of course it is because I'm missing something):
On the one hand, it seems to me that when the contraction of the spring is maximal, all the kinetic energy of $A$ was transformed to a potential elastic energy and to some portion of kinetic energy of $B$ (it will move faster). So it means that $A$ has no velocity at that moment, but $B$ has, so it's impossible that their relative velocity is zero. Intuitively, it means that $A$ has some kinetic energy. So it means that $A$ will be slowed down by the spring, until its velocity is zero, and the spring can finally return to its normal state. So it seems that the spring will be contracted maximally for some relatively short period of time until the kinetic energy of $A$ becomes zero. But something tells me that I'm wrong. Anyway, I've tried to play with the law of conservation of energy and to wrap it around my thoughts, but it didn't quite work well. So what am I missing? I will appreciate any help.

Proposed solution:
Relative speed during the maximal contraction
The moment $A$ reaches the spring, the spring will push both of the bodies as a reaction to the $A$'s push. Therefore, the $A$ will start to decelerate while $B$ will start to gain more speed. Although $A$ is losing its kinetic energy, its velocity won't be zero at the moment of the maximal contraction. This is because $A$ will continue to contract the spring as long as its speed is higher than the speed of $B$ (and therefore the speed of the spring itself). The moment their velocities are the same, $A$ will no longer be "overtaking" the spring, therefore its contraction will be maximal at that point. Therefore, the relative speed is zero.
Finding the maximal contraction:
The net momentum is conserved at any point in the time, so:
$m_A v_A + m_B v_B = (m_A + m_B) V$
In this particular question, $V$ would be: $5.5 \text{m/s}$
We also know that the total mechanical energy is conserved (the collision is perfectly elastic), therefore, the mechanical energy before the collision (which only consists of kinetic energies) will be equal to the total mechanical energy during the max contract. Therefore:
$\frac{1}{2} m_A v_A^2 + \frac{1}{2} m_B v_B^2 = \frac{1}{2} (m_A + m_B) V^2 + \frac{1}{2} k (\Delta x_{max})^2$
The energy when the contraction is maximal consists of kinetic energies of $A$ and $B$ (which are moving with the same velocity) and of elastic potential energy due to the fact that the spring is contracted.
In this particular question $\Delta x_{max}$ would be:
$\Delta x_{max} = \sqrt{\frac{27}{400}} \approx 26 \text{cm}$
 A: Once A comes into contact with the spring, A's velocity relative to B will be the rate at which the spring is contracting. When the spring achieves its maximum contaction, the rate of contraction is zero, and thereby A's velocity relative to B is also zero.
A: when the contraction of the spring is maximal, it is not true that $A$ is not moving (in the lab frame).
The trouble is that the spring takes some time to accelerate $B$, so as $A$ is slowing down, $B$ is simultaneously speeding up. So you are right that when $A$ has finally stopped, $B$ is moving. But precisely because $B$ started to move from the moment the spring touches it, it has somehow 'moved too much' and extended the spring, so the spring is not at a minimal length when $A$ finally stops.
In fact, note that the assumption that $A$ stops is not true in general, although this is true in this question ($A$ does stop). Imagine $A$ being very massive and $B$ being very light. Then $A$ just keeps going like a steamroller, without stopping, while $B$ recoils off $A$ extremely quickly. There is still a maximum contraction of the spring - but surely this cannot depend on the fact that $A$ stops. So if you believe that the statement that the question is making is not specific to the case of $m_A = 2kg$ and $m_B = 6kg$, the argument you come up with cannot depend on the fact that $A$ stops.
Ok so how do you do it?
Besides conservation of energy, there is also conservation of momentum (wiki it), because the system is isolated. That is, the total momentum of $A$ and $B$ is a constant. What that means is that the center of mass (Wikipedia it if you don't know what it is!) is always moving at a constant velocity $v_{com}$. 
Now it is easiest to analyze this in the frame of reference (Wikipedia it) of the center of mass. That is, imagine you are in a car moving next to the blocks at $v_{com}$, and looking at $A$ and $B$. So you see the center of mass stationary at all times, since you are moving along with it in the lab frame, while you see $A$ and $B$ come towards each other.
In this case, both $A$ and $B$ will necessarily stop (why? and do they stop at the same time?). The spring is therefore maximally compressed then.
Moving back to the lab frame (just jumping off the car at that instant and standing on the ground), you see that the car, the center of mass, and both $A$ and $B$ are all moving at $v_{com}$. Thus the relative velocity between $A$ and $B$ is 0.
You should try to convince yourself of my statements. There are of course many ways to answer the question, like setting up a function of length of spring vs. relative speed and differentiating, but it is less physical than the one I've given. Happy learning!
