List All Positive Divisors of an Integer in Ascending Order, Then Beat Square-Root Time
Company: PDT Partners
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Given a positive integer up to one trillion, return every positive divisor in ascending order, each listed once. The task tests divisor enumeration, correct handling of perfect squares and 64-bit values, and a follow-up asking whether the running time can grow more slowly than the square root of n.
Read the full PDT Partners Machine Learning Engineer interview experience this question came from
Constraints
- 1 <= n <= 10^12
- n can exceed 2^31 - 1, so use 64-bit integers in languages with fixed-width integer types (Java long, C++ long long); every input and every returned divisor is at most 10^12, well within 2^53.
Examples
Input: (1,)
Expected Output: [1]
Explanation: Minimum valid n: 1 is its own only divisor, so the first and last element coincide and must not be duplicated.
Input: (2,)
Expected Output: [1, 2]
Explanation: Smallest prime: only 1 and n.
Hints
- Testing every candidate from 1 to n is far too slow when n is close to 10^12. Which candidates do you actually need to test?
- Example 1 points out that 36 is a perfect square and 6 appears only once. Make sure your method neither drops nor duplicates such a divisor.
- n can exceed 2^31 - 1, so check that your integer types and any loop-bound arithmetic cannot overflow.