Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

Coding · Binary search

Least time for a workshop to repair every car

Medium · Target 25 minutes · Binary search · Type asked atGoogleHudson River Trading

A workshop has mechanics with ranks ranks[i]. A mechanic of rank r needs r × n² minutes to repair n cars. All mechanics work at the same time. Return the minimum number of minutes in which cars cars can all be repaired.

Examples

InputOutputWhy
ranks = [4, 2, 3, 1], cars = 1016In 16 minutes: 2 + 2 + 2 + 4 = 10 cars (rank 4 does 2 in 16; rank 1 does 4 in 16).
ranks = [5, 1, 8], cars = 616Rank 5: 1 car (5 min); rank 1: 4 cars (16 min); rank 8: 1 car (8 min).

Constraints

  • 1 ≤ ranks.length ≤ 10⁵
  • 1 ≤ ranks[i] ≤ 100
  • 1 ≤ cars ≤ 10⁶

Hints

Hint 1

Given t minutes, how many cars can a mechanic of rank r repair?

Hint 2

The largest n with r × n² ≤ t, which is ⌊√(t / r)⌋. Compute it with integers.

Hint 3

The total over all mechanics never falls as t grows. Search t in [1, min(ranks) × cars²].

Where people lose marks

  • Using floating-point square roots without correcting them: an answer off by one minute fails the tests.
  • Taking √(t) / r instead of √(t / r).
  • Starting with hi = max(ranks) × cars², which works but wastes steps, or with a bound that fits in 32 bits, which does not.
Console⌘/Ctrl + Enter runs

Write a solution and run it against the real test table.


Keep going

Preparing for a real process? Quant interview preparation, or book a free 20-minute call.