This question evaluates a candidate's competency in implementing binary search and handling integer arithmetic and overflow considerations when checking numeric properties. Commonly asked to assess algorithmic complexity analysis and practical coding ability within the Coding & Algorithms domain, it emphasizes practical application and O(log n) performance rather than purely conceptual reasoning.
Given a positive integer n, determine whether it is a perfect square (i.e., there exists an integer x such that x*x = n).
O(log n)
approach.
n
(
1 <= n <= 2^31 - 1
).
true
if
n
is a perfect square, otherwise
false
.
n = 16
->
true
(4×4)
n = 14
->
false
mid * mid
.