Find perfect squares in range
The only reasons I can think of that your code was deemed incorrect are that it doesn't consider 0 as a perfect square, and that you don't consider the possibility that B &lt; A (although maybe the test defined B as being greater than or equal to A and this is not a negligence of you). As for the performance, here are some suggestions:


You don't need to check i * i &lt;= B in the loop, because the termination condition of the for loop already required that i &lt;= upperLimit, and with i being positive, there is no way that i*i &gt; B if i &lt;= sqrt(B).
Taking the above into account, it follows that once the loop reaches an i for which i * i &gt;= A, there is no point in continuing the loop, because all future values of i will have squares that fall within the specified range.
Continuing this trail of thought, it turns out that the solution can simply be expressed as (int) (Math.floor(Math.sqrt(B)) - Math.ceil(Math.sqrt(Math.max(A, 0)))) + 1, provided B is non-negative and B &gt;= A. This expression doesn't really do anything your code doesn't do except floating point arithmetics. I don't know if two calls to Math.sqrt() (which delegates to StrictMath.sqrt(), which is a native method) are faster than using integer arithmetics in a loop, but it's definitely more to the point, coding-wise.