By compressing the spring a distance x (in m), you are storing 1/2 k x ² (in J) of potential energy, which is converted completely into kinetic energy 1/2 m v ², where
• k = 40 N/m = spring constant
• m = 10 kg = mass of the ball
• v = 2 m/s = ball's speed (at the moment the spring returns to its equilibrium point)
So we have
1/2 k x ² = 1/2 m v ²
x = √(m/k v ²) = √((10 kg) / (40 N/m) (2 m/s)²) = 1 m