Check Whether a Number Is a Perfect Square
Decide whether a number is the exact square of some integer.
A number is a perfect square when some integer squared equals it. Take the integer square root, then verify by squaring it back. For 49 the integer root is 7 and 7 * 7 == 49, so 49 is a perfect square; 50 fails the check.
Problem Statement
Given a non-negative integer n, decide whether it is a perfect square — that
is, whether there is an integer r such that r * r == n. Return true or
false.
For example, 49 is 7 * 7, so it is a perfect square, while 50 is not the
square of any integer.
Input: A single non-negative integer n.
Output: A boolean: true if n is a perfect square, otherwise false.
Examples
Input: 49
Output: true7 * 7 = 49, so 49 is a perfect square.
Input: 50
Output: false7 * 7 = 49 and 8 * 8 = 64, so no integer squares to 50.
Constraints
0 <= n and fits in a 32-bit intGuard against floating-point rounding when using sqrt
Think Before You Code
Reveal the questions to ask yourself first
- How can the square root give you a candidate integer to test?
- Why is it unsafe to trust
Math.sqrtfor an exact equality on its own? - How do you verify a candidate root using only integer arithmetic?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Take a candidate root and verify it with integer math.
- Reject negative
noutright. - Compute
r = (int) Math.sqrt(n). - Return
trueifr * r == nor(r + 1) * (r + 1) == n, elsefalse.
Checking r + 1 guards against the rare case where floating-point rounding
nudges the square root just below the true integer value.
Dry Run
Walk through the example step by step
Checking n = 49:
r = (int) Math.sqrt(49) = 7
r * r = 7 * 7 = 49
49 == 49 -> true
Checking n = 50:
r = (int) Math.sqrt(50) = 7
r * r = 49 (not 50)
(r+1)*(r+1) = 64 (not 50)
-> false
Solution
Reveal the full Java solution
public class PerfectSquare {
public static boolean isPerfectSquare(int n) {
if (n < 0) {
return false;
}
int r = (int) Math.sqrt(n);
return r * r == n || (r + 1) * (r + 1) == n;
}
public static void main(String[] args) {
System.out.println(isPerfectSquare(49)); // true
System.out.println(isPerfectSquare(50)); // false
}
}
Math.sqrt returns a double, which can be a hair below the true root for large
perfect squares, so casting to int might land one short. Verifying with integer
multiplication — and also checking r + 1 — removes any dependence on the exact
floating-point value, making the test reliable across the whole int range.
O(1)Space: O(1)Common Mistakes
- Comparing `Math.sqrt(n)` to its rounded value with doubles, which is fragile.
- Forgetting the `r + 1` check, causing rare false negatives from rounding.
Edge Cases to Test
- n = 0 is a perfect square (0 * 0).
- n = 1 is a perfect square (1 * 1).
Interview Follow-Ups
- How would you test for a perfect square without any floating-point at all (binary search)?
- How would you extend this to detect a perfect cube?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →