PPractick

DSA interview question

Find the Smallest Divisor Given a Threshold

Medium · Algorithms · Topics: binary search on answer, array

The problem

A load balancer splits each incoming job of size nums[i] into chunks of at most d units, and each chunk costs one scheduling slot: ceil(nums[i] / d) slots per job. The operations team wants the smallest chunk size d that keeps the total slot count within a threshold, because smaller chunks balance the load better. A larger d always means fewer (or equal) slots, so feasibility is monotonic, and binary search on d between 1 and the largest job finds the answer.

Example: nums = [1, 2, 5, 9], threshold 6 → 5.

Function signature

int smallestDivisor(const vector<int>& nums, long long threshold);  // threshold >= nums.size()

Test cases

Input (nums, threshold)OutputExplanation
([1, 2, 5, 9], 6)51 + 1 + 1 + 2 = 5 ≤ 6; with d = 4 the sum is 7
([44, 22, 33, 11, 1], 5)44Each element must round up to exactly 1
([21212, 10101, 12121], 1000000)1Even d = 1 is within the threshold
([2, 3, 5, 7, 11], 11)31 + 1 + 2 + 3 + 4 = 11

(Confirm: threshold ≥ number of elements, so d = max(nums) always works; each term is ceil(nums[i] / d).)

How interviewers phrase it

Clarifying questions to ask

  1. Is each division rounded up?
  2. Is the threshold guaranteed to be at least the number of elements?
  3. How large can the values be? (The sum needs 64 bits.)

Follow-ups to expect

  1. "Why is it monotonic?"
  2. "What's the search range?"
  3. "How is this the same as Koko?"
  4. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in