Heaters

MediumBinary Searchbinary search

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Houses and heaters sit at integer positions on a line, given as arrays houses and heaters (both unsorted, duplicates allowed). Every heater warms all positions within distance r of it, and all heaters share the same radius r. Return the **minimum** integer radius r such that every house is within distance r of at least one heater. Aim for O((n + m) log m) time, where n and m are the numbers of houses and heaters.

Examples

Input: houses = [1,2,3], heaters = [2]
Output: 1
The heater at 2 must reach houses 1 and 3, each at distance 1.
Input: houses = [1,2,3,4], heaters = [1,4]
Output: 1
House 2 is 1 from the heater at 1 and house 3 is 1 from the heater at 4.
Input: houses = [1,5], heaters = [2]
Output: 3
House 5 is 3 away from the only heater.

Constraints

  • 1 <= houses.length, heaters.length <= 3 * 10^4
  • 1 <= houses[i], heaters[i] <= 10^9

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More binary search problems