Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Split Integer Into Parts

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

Your question is Split Integer Into Parts. 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

LogicMonitor may need to distribute an integer number of alert checks across an even number of workers. Given an integer value and a positive even integer parts, return an array of exactly parts integers whose sum is value and whose values are as evenly balanced as possible.

The output must minimize the difference between the largest and smallest parts. When multiple valid arrays exist, return the deterministic arrangement with larger parts first. For negative values, "larger" means numerically greater, so -2 comes before -3.

Formal Specification

Implement split_integer(value, parts).

  • Input: integer value and positive even integer parts.
  • Output: an array of parts integers.
  • The array elements must sum to value.
  • The maximum difference between any two elements must be minimized.

Constraints

  • -10^9 <= value <= 10^9
  • 2 <= parts <= 10^5
  • parts is a positive even integer
  • The returned array must contain exactly parts integers

Function Signature

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