FastPrepSpread Cows Across Stalls
Problems/Spread Cows Across Stalls

Spread Cows Across Stalls

Expedia logoExpedia● MediumINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: An anonymous entry for an Expedia Group SDE intern campus process names this task in its online coding round, on July 20, 2020.

The candidate's text has only a short tip on method. A separate card for the same title gives the task, examples and limits: 2 to 10^5 stalls at positions from 0 to 10^9, and 2 to n cows. Neither text says whether two stalls can share a position. Here, positions are all different, and there are at most 10^4 stalls so that the test inputs stay small. Distances can still reach 10^9, so trying every distance one by one is too slow.

Our version is about 90% the same as the reported question.

A barn has n stalls along a straight line. Stall i is at position[i]; the positions are not given in any particular order. You must put m cows into m different stalls.

The cows do not like being close to each other, so place them to make the smallest distance between any two cows as large as possible. Return that largest possible smallest distance.

Function

maxDistance(position: int[], m: int) → int

Examples

Example 1

position = [9,1,5,13,4]m = 3return = 4

Sorted, the stalls are at 1, 4, 5, 9 and 13. Cows at 1, 5 and 9 are 4 apart. A smallest gap of 5 is not possible: after a cow at 1, the next cow needs a stall at 6 or more (9), and the third would need 14 or more, which does not exist. So the answer is 4.

Example 2

position = [2,30,7,21]m = 2return = 28

With two cows, put them at the two ends, 2 and 30. Their distance is 28.

Example 3

position = [0,6,3,11,12,18,8]m = 4return = 6

Sorted, the stalls are at 0, 3, 6, 8, 11, 12 and 18. Cows at 0, 6, 12 and 18 are all 6 apart. A smallest gap of 7 is not possible: after a cow at 0, the next cow needs a stall at 7 or more (8), the third needs 15 or more (18), and a fourth would need 25 or more, which does not exist. So the answer is 6.

Constraints

  • 2 <= position.length <= 10^4
  • 0 <= position[i] <= 10^9
  • All values in position are different.
  • 2 <= m <= position.length

More Expedia Online Assessment questions

  • Balanced NumbersOA · Seen Aug 2026
  • REST API: Discounted PriceOA · Seen Aug 2026
  • Maximum Team SizeOA · Seen Mar 2026
  • Per-Domain Request LimiterOA · Seen Oct 2025
  • Count Equal-Frequency SubsequencesOA · Seen Mar 2025
  • Count Increasing Stock PeriodsOA · Seen Mar 2025
  • Get Minimum StepsOA · Seen Oct 2024
  • Rank Songs by PopularityOA · Seen Oct 2024
  • Calculate the SumOA · Seen Oct 2024
  • Get Maximum EfficiencyOA · Seen Oct 2024

Similar questions at other companies

  • Minimize Maximum Group DifferenceDRW · OADRW interview questions
  • Minimum-Cost Solar Rooftop AllocationDeloitte · OADeloitte interview questions
  • Maximize Number of Produced CarsMicrosoft · OAMicrosoft interview questions
  • Aggressive CowsPeopleStrong · OAPeopleStrong interview questions
  • Aggressive CowsAmazon · PHONE SCREEN
  • Maximize the Minimum Cone DistanceSumo Logic · PHONE SCREEN
  • Maximize Fish over Limited HoursByteDance · PHONE SCREENByteDance interview questions
  • Find Minimum TimeAlphaSense · OA
  • Find Minimum TimeBlackRock · OABlackRock interview questions
  • Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions

Expedia interview experiences

  • Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
  • Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
  • Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
  • Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
  • Expedia Intern Final Round in 2023: 17 Experiences (Part 2 of 6)
  • Expedia SWE Intern Final Round: 126 Experiences (Part 1 of 6)
All Expedia interview questionsSee Expedia hiring insights
Saved
class Solution {
    public int maxDistance(int[] position, int m) {
        // Write your code here.
    }
}
Checking account…