Step-by-step explanation:
Spring plus a sphere is shown in the free body diagram below
we know that spring obeys hook's law
that is given by

here, F is the applied force, k is the spring constant and x is compression or elongation in the spring. negative sign shows that spring force always acts in the negative direction.
From the free-body diagram
for the first position, we can see that compression in the spring is small
to press the sphere against the spring we apply some force

we assume that spring is compressed by some value say

then from the hook's law, we can write

Similarly at position B we apply some greater force say

and spring compressed by some value say

then, from the hooks' law

as we can see that there is a linear relationship between F and x.
Similarly for position A can also be understood.
we can also use energy conservation here


the more we pressed the sphere against the spring the fast it moves.
this method will also give the same value of spring constant provided you correctly measure the speed of the object after released.