I think your answer may be wrong.
The mass's potential energy at the start is mgh; this gets converted to kinetic energy as it falls (assuming no air resistance), so that by conservation of energy,
mgh = 1/2 mv² ⇒ v = √(2gh)
where v is the velocity of the mass when it first comes into contact witht he spring.
As the spring is compressed, it performs work on the mass as it slows to a rest at maximum compression. If x is the maximum compression, then the spring does -1/2 kx² of work. (negative since it opposes the downward fall)
By the work-energy theorem (total work is equal to change in kinetic energy),
-1/2 kx² = 0 - 1/2 mv²
Plug in v and solve for x :
kx² = m (√(2gh))²
x² = 2mgh/k
x = √(2mgh/k)