Quick Overview

Decide whether a positive 32-bit integer is a perfect square without a built-in square-root function. Explain how your approach stays within logarithmic time.

Check Whether an Integer Is a Perfect Square

Company: LinkedIn

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Perfect Square Check Given a positive integer `n`, determine whether it is a **perfect square** (i.e., there exists an integer `x` such that `x*x = n`). ### Requirements - Do **not** use built-in square-root functions. - Implement an `O(log n)` approach. ### Input - Integer `n` (`1 <= n <= 2^31 - 1`). ### Output - Return `true` if `n` is a perfect square, otherwise `false`. ### Examples - `n = 16` -> `true` (4×4) - `n = 14` -> `false` ### Notes - A candidate square can exceed the signed 32-bit range.

Overview: Decide whether a positive 32-bit integer is a perfect square without a built-in square-root function. Explain how your approach stays within logarithmic time.

Return whether n is a perfect square using binary search without sqrt.

Constraints

  • Inputs are provided as Python literals matching the function signature.
  • Return a deterministic exact-match result.

Examples

Input: (16,)

Expected Output: True

Explanation: Perfect square.

Input: (14,)

Expected Output: False

Explanation: Not a square.

Hints

  1. Choose a representation that makes the core operation simple.
  2. Handle empty and boundary inputs before the main algorithm.

Community answers

Answer by Wingadiam-Leviosaa

left = 1 right = num//2 if num==1: return True while left<=right: mid = (left+right)//2 if midmid==num: return True elif midmid<num: left = mid+1 else: right = mid-1 return False

Answer by Wingadiam-Leviosaa

You sir, need to fix your comments section

Loading coding console...