Smallest free hub for a task
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Amazon. It tests intervals, binary-search and scheduling.
You run a fleet of n compute hubs, numbered 0 to n - 1, and need to place one new task on a single hub.
You are given:
-
capacities: an integer list of lengthn.capacities[i]is the processing capacity of hubi. -
intervals: a list ofnlists of strings.intervals[i]holds the time ranges already booked on hubi, each written"start:end"with integer times. Both ends are inclusive. -
requiredCapacity: the capacity the task needs. -
taskInterval: the task's time range, also"start:end"and inclusive.
A hub can take the task when its capacity is at least requiredCapacity and none of its bookings overlaps the task interval. Because ends are inclusive, two ranges that share even one time unit overlap: a booking "4:6" blocks a task "6:9".
Return the index of the hub that can take the task and has the smallest capacity. If several qualify with the same capacity, return the smallest index. Return -1 if no hub can take it.
find_smallest_hub(capacities, intervals, requiredCapacity, taskInterval) -> int
Constraints: 0 <= n <= 10^5; total bookings up to 2 * 10^5; times fit in a 32-bit integer and start <= end. Bookings on one hub may be unsorted.
Example 1
capacities = [9, 3, 5]
intervals = [[], ["0:2"], ["7:8"]]
requiredCapacity = 3
taskInterval = "1:4"
Hub 1 is busy (0:2 overlaps 1:4). Hubs 0 and 2 are free, and hub 2 has the smaller capacity. Answer: 2.
Example 2
Same hubs, taskInterval = "3:6". Hub 1 is now free and is the smallest that fits. Answer: 1.
Part 2. Requests arrive one after another. Each accepted task is booked on its chosen hub, so it blocks later requests. Design a class with schedule(requiredCapacity, taskInterval) -> int that is fast across many calls.
Clarifying questions
- Can a booking string be malformed, and should I validate it?
- Does a hub with zero capacity ever qualify (only if
requiredCapacity <= 0)? - In part 2, are tasks ever cancelled, and are requests in time order?
- How many requests and hubs should part 2 handle, roughly?
What a strong answer covers
- Part 1: parses each booking once and checks overlap with the inclusive rule
a_start <= b_end and b_start <= a_end; one pass over all hubs and bookings, O(n + total bookings), keeping the smallest capacity and then the smallest index. - Gets the inclusive boundary right:
4:6blocks6:9. - Part 2: keeps each hub's bookings sorted (a sorted list of starts), so the overlap check is a binary search, O(log b); orders hubs by (capacity, index) and scans from the first hub with enough capacity until one is free.
- Names the worst case of that scan (many busy hubs of the right size) and how to bound it, for example a segment tree over hubs in capacity order or grouping hubs by capacity.
- Tests both examples,
n = 0, ties on capacity, touching intervals and unsorted bookings.
Follow-ups
- Cancel a booked task.
- Allow a task to be split across several hubs whose capacities add up.
- Choose the hub that minimises fragmentation of its free time, not just capacity.
Where it's asked
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
