Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Array Factors Coding

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Array Factors Coding. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Paycom Beti may need small numeric utilities when validating payroll-related calculations. Given a positive integer n, return an array containing every positive factor of n exactly once, sorted in ascending order.

Use factor pairs to avoid checking every value up to n. If d divides n, then both d and n // d are factors. Be careful not to add the same factor twice when n is a perfect square.

Formal Specification

Implement factors(n).

  • Input: An integer n, where n >= 1.
  • Output: A list of all positive integers that divide n evenly, in strictly ascending order.

Constraints

  • 1 <= n <= 10^12
  • Return every positive factor exactly once
  • Return factors in strictly ascending order
  • The algorithm should run in better than O(n) time

Function Signature

def factors(n):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output