Sign in to track progress, unlock prep layers, and browse your pipeline.Sign in
Check perfect square using binary search
Perfect Square Check
Given a positive integer n, determine whether it is a perfect square. A perfect square is an integer x such that x*x = n.
Requirements
- You must not use any built-in square-root functions.
- The implemented solution must have an
O(log n)time complexity.
Input
- An integer
nwhere1 <= n <= 2^31 - 1.
Output
- Return
trueifnis a perfect square, otherwisefalse.
Examples
n = 16->true(since 4 * 4 = 16)n = 14->false
Important Considerations
- Pay close attention to potential integer overflow when computing intermediate products like
mid * mid.
AI interview prep
Core dilemma
Efficiently locating the integer square root within a large numerical range, strictly adhering to logarithmic time complexity, and robustly managing potential integer overflows.
Mid-level signals3
Senior signals3
Red flags4
2 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Check perfect square using binary search
Perfect Square Check
Given a positive integer n, determine whether it is a perfect square. A perfect square is an integer x such that x*x = n.
Requirements
- You must not use any built-in square-root functions.
- The implemented solution must have an
O(log n)time complexity.
Input
- An integer
nwhere1 <= n <= 2^31 - 1.
Output
- Return
trueifnis a perfect square, otherwisefalse.
Examples
n = 16->true(since 4 * 4 = 16)n = 14->false
Important Considerations
- Pay close attention to potential integer overflow when computing intermediate products like
mid * mid.