FastPrepSmallest Batch Size Within a Step Limit

Smallest Batch Size Within a Step Limit

Microsoft logoMicrosoft● MediumINTERNONSITE INTERVIEW

Problem statement

You are given an integer array sizes and an integer maxBatchCount.

You pick one positive integer batch size b. Each number x in sizes is processed b at a time, so it needs x / b steps, rounded up. For example, with b = 4 the number 5 needs 2 steps and the number 3 needs 1 step.

The total number of steps is the sum over all numbers in sizes. Return the smallest batch size b for which the total is at most maxBatchCount.

Such a batch size always exists: when b is the largest number in sizes, every number needs one step, and maxBatchCount is at least the number of elements.

Function

minBatchSize(sizes: int[], maxBatchCount: int) → int

Examples

Example 1

sizes = [1,3,5,5]maxBatchCount = 6return = 3

With batch size 2 the steps are 1, 2, 3, 3, which is 9 steps, more than 6. With batch size 3 the steps are 1, 1, 2, 2, which is exactly 6 steps, so it is allowed. Batch size 4 also gives 1, 1, 2, 2 = 6 steps, but 3 is smaller, so the answer is 3.

Example 2

sizes = [1,3,5,5]maxBatchCount = 4return = 5

Only 4 steps are allowed for 4 numbers, so every number must finish in one step. That needs a batch size of at least 5, the largest number. With batch size 4 the steps are 1, 1, 2, 2 = 6, which is too many. The answer is 5.

Example 3

sizes = [9,2,4]maxBatchCount = 15return = 1

With batch size 1 each number needs as many steps as its value: 9 + 2 + 4 = 15. That fits the limit of 15, so the answer is 1.

Constraints

  • 1 <= sizes.length <= 10^5
  • 1 <= sizes[i] <= 10^9
  • sizes.length <= maxBatchCount <= 10^9

More Microsoft problems

See Microsoft hiring insights
Saved
public int minBatchSize(int[] sizes, int maxBatchCount) {
    // Write your code here.
}
Checking account…