← LinkedIn Interview Insights

LinkedIn·Mobile Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

LinkedIn mobile engineering interview with a coding problem that looked simple on the surface but had a specific constraint that tripped me up a bit. Binary search was the expected approach and they were pretty clear about the O(log n) requirement.

Questions Asked (1)

Q1

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

Algorithms & Data Structures
Author's notes

My first instinct was to just iterate from 1 upward and check each square, which obviously fails the time constraint.

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 whose square equals n. At each step, compare mid*mid with n and adjust the search bounds accordingly. This yields O(log n) time and avoids built-in square root functions.

Pro tip: Mention that you can optimize the search space to [1, n/2] for n > 1, and use long long to prevent integer overflow when computing mid*mid. This shows attention to edge cases and performance.

1. Clarify constraints and edge cases

Confirm that n is a positive integer and discuss handling of n=1 (perfect square) and n=0 if allowed. Also address potential integer overflow in mid*mid.

2. Choose binary search bounds

Set low=1 and high=n (or n/2 for n>1) to cover all possible integer square roots. Explain why these bounds are valid.

3. Implement binary search loop

While low <= high, compute mid, then compare mid*mid with n. If equal, return true; if less, set low=mid+1; else set high=mid-1.

4. Handle termination and return result

If the loop ends without finding an exact square, return false. Discuss why the algorithm correctly determines non-squares.

5. Analyze complexity and test

State that time complexity is O(log n) due to halving the search space, and space is O(1). Suggest testing with perfect squares, non-squares, and large values.

Key Points to Mention

  • Binary search on the integer range to achieve O(log n) time.
  • Avoiding built-in square root functions by using multiplication and comparison.
  • Preventing integer overflow by using long long or equivalent for mid*mid.
  • Optimizing the upper bound to n/2 for n > 1 to reduce iterations.
  • Handling edge cases such as n=1 and n=0 (if allowed).
  • Explaining why the algorithm is correct and terminates.

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