← LinkedIn Interview Insights

LinkedIn·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026

Summary

LinkedIn Software Engineer interview with a binary search coding problem. Pretty focused session, just the one algorithmic question but it had a few gotchas worth knowing about.

Questions Asked (1)

Q1

Given a positive integer n, determine if it is a perfect square without using any built-in square root functions. Your solution should run in O(log n) time.

Algorithms & Data Structures
Author's notes

The binary search angle is pretty clear once you think about it for a second, but the part that actually tripped me up was the overflow.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use binary search on the range [1, n] to find an integer x such that x^2 equals n. At each step, compute mid^2 and compare with n, adjusting the search range accordingly. This yields O(log n) time and avoids built-in square root functions.

Pro tip: Mention that you can optimize by setting the upper bound to n/2 for n > 1, and use integer division to avoid overflow when computing mid. Also, clarify that you're using binary search on the answer space, not on the input array.

1. Clarify constraints and edge cases

Confirm that n is a positive integer and discuss edge cases like n=1 (perfect square) and n=2 (not). Also consider potential integer overflow when squaring mid.

2. Choose binary search bounds

Set low=1 and high=n. Optionally, optimize high to n/2 for n>1 to reduce search space, but ensure correctness for n=1.

3. Implement binary search loop

While low <= high, compute mid = low + (high - low)/2. Compare mid*mid with n: if equal, return true; if less, set low = mid+1; if greater, set high = mid-1.

4. Handle termination and return result

If the loop ends without finding a perfect square, return false. Ensure the algorithm runs in O(log n) time.

5. Analyze complexity and test

Explain that the search space halves each iteration, giving O(log n) time and O(1) space. Walk through examples like n=16 (true) and n=14 (false).

Key Points to Mention

  • Binary search on the answer space [1, n]
  • Time complexity O(log n) due to halving the search range
  • Space complexity O(1)
  • Avoiding integer overflow by using mid <= n / mid or long integers
  • Edge cases: n=1, n=2, large n near integer limits
  • Alternative approaches like Newton's method (but binary search is simpler and meets O(log n))

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.