Skip to content

ORIGINAL CODING QUESTION

Spread interviews apart

A recruiter has n open time slots, given as distinct integer minutes in no particular order, and must book exactly c interviews in c different slots. Choose the slots so that the smallest gap between any two booked interviews is as large as possible, and return that smallest gap.

Open practice workspace

Original exercise · medium. Editorial role relevance does not imply a company has asked this question.

Input and output

n c, then n distinct slot times. Print the largest achievable minimum gap.

Constraints

2 ≤ c ≤ n ≤ 200; 0 ≤ slot time ≤ 1,000,000.

Example 1

Input

5 3
1 2 8 4 9

Output

3

Booking 1, 4 and 8 keeps every gap at least 3; a minimum gap of 4 is impossible.

Example 2

Input

4 4
10 0 30 20

Output

10

All four slots are booked; the closest pair is 10 apart.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Sort the slots first.

Hint 2

For a proposed gap, book greedily from the earliest slot, taking each slot at least that far from the last booking.

Hint 3

Feasibility is monotone in the gap, so binary search for the largest gap whose greedy count reaches c.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →