PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of integer representations as sums of two squares and the ability to reason about counting distinct unordered representations, drawing on elementary number theory and algorithmic enumeration.

  • hard
  • Google
  • Coding & Algorithms
  • Software Engineer

Find numbers with exactly two sum-of-squares forms

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

Given an integer \(n\), find all integers \(s\) such that \(0 \le s < n\) and \(s\) can be expressed as a sum of two squares in **exactly two distinct unordered ways**: \[ s = a^2 + b^2 \] where \(a\) and \(b\) are **non-negative integers**, and \((a,b)\) and \((b,a)\) are considered the **same** representation (i.e., order does not matter). Return all such \(s\) (in any order, unless otherwise specified). ### Example - For \(n > 50\), \(50\) qualifies because: - \(50 = 1^2 + 7^2\) - \(50 = 5^2 + 5^2\) and there are exactly two unordered representations. ### Constraints - \(1 \le n < 2^{31}-1\) ### Notes - Representations must use **non-negative** integers. - Count unordered pairs: enforce \(a \le b\) to avoid double-counting.

Quick Answer: This question evaluates understanding of integer representations as sums of two squares and the ability to reason about counting distinct unordered representations, drawing on elementary number theory and algorithmic enumeration.

Given an integer `n`, find all integers `s` such that `0 <= s < n` and `s` can be expressed as a sum of two squares ``` s = a^2 + b^2 ``` in **exactly two distinct unordered ways**, where `a` and `b` are **non-negative** integers and `(a, b)` and `(b, a)` count as the **same** representation (enforce `a <= b` to avoid double-counting). Return the qualifying values in **ascending order**. ### Example `n = 51` -> `[25, 50]` - `25 = 0^2 + 5^2 = 3^2 + 4^2` - `50 = 1^2 + 7^2 = 5^2 + 5^2` Each has exactly two unordered representations, and both are `< 51`. ### Approach Enumerate every unordered pair `(a, b)` with `0 <= a <= b` whose squared sum is `< n`, tally how many representations each sum receives in a hash map, then return every sum whose count is exactly `2`. Bounding the loops by `a*a < n` and `a*a + b*b < n` keeps the work near `O(n)` pairs. ### Constraints - `1 <= n < 2^31 - 1` - Representations use non-negative integers; order does not matter.

Constraints

  • 1 <= n < 2^31 - 1
  • a and b are non-negative integers
  • (a, b) and (b, a) are the same representation (enforce a <= b)
  • Return values in ascending order

Examples

Input: (51,)

Expected Output: [25, 50]

Explanation: 25 = 0^2+5^2 = 3^2+4^2 and 50 = 1^2+7^2 = 5^2+5^2; both have exactly two unordered forms and are < 51.

Input: (100,)

Expected Output: [25, 50, 65, 85]

Explanation: Adds 65 = 1^2+8^2 = 4^2+7^2 and 85 = 2^2+9^2 = 6^2+7^2, each with exactly two representations below 100.

Input: (66,)

Expected Output: [25, 50, 65]

Explanation: 65 now qualifies (65 < 66) but 85 does not, since 85 >= 66.

Input: (26,)

Expected Output: [25]

Explanation: The bound is exclusive: 25 < 26 is included, while 50 is excluded.

Input: (25,)

Expected Output: []

Explanation: 25 is not < 25, so no value in [0, 25) has exactly two representations.

Input: (2,)

Expected Output: []

Explanation: Only s in {0, 1} are in range; neither has two distinct sum-of-squares forms.

Input: (1,)

Expected Output: []

Explanation: Only s = 0 is in range, which has a single representation 0^2 + 0^2.

Input: (0,)

Expected Output: []

Explanation: Empty range; nothing to return.

Hints

  1. Brute-forcing every value s and re-deriving its representations is wasteful. Instead, generate the representations once: iterate every unordered pair (a, b) with 0 <= a <= b and a^2 + b^2 < n, and tally each resulting sum in a hash map.
  2. Bound the outer loop by a*a < n and the inner loop by a*a + b*b < n so you never exceed the range, and start b at a (not 0) so (a, b) and (b, a) are counted once.
  3. After tallying, the answer is every sum whose count equals exactly 2. Sort before returning for a deterministic order.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Infection Spread on a Grid (Cellular Automaton) - Google (hard)
  • Boolean Expression Tree with Leaf Flips - Google (medium)
  • Streaming Points: Remove Any Pair Within a Distance - Google (medium)
  • Most Active Users in a Live Communication Stream - Google (medium)
  • Solve Rooms and Top-K Streams - Google (medium)