Average speed at a point on a road with overlapping reports
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Waymo. It tests segment tree, range update and data structure design.
A straight road is modeled as the number line from position 0 to 10^9. Vehicles send speed reports. Each report says that traffic on the half-open segment [start, end) moved at speed, so it covers every integer position p with start <= p < end. Reports can overlap.
Implement RoadwaySpeedTracker:
-
RoadwaySpeedTracker()creates an empty tracker. -
add(start: int, end: int, speed: float) -> intrecords a report and returns a unique id for it.0 <= start < end <= 10^9 + 1. -
remove(report_id: int) -> Noneremoves a report added earlier. Removing an unknown or already removed id is an error. -
query(position: int) -> floatreturns the average speed atposition: the mean of the speeds of all active reports that cover it. If no report covers it, return0.0.
The average is a plain mean over reports, not weighted by segment length.
Constraints
- Up to
10^5calls in total, in any order. - Calls arrive online: you do not know future coordinates.
- Aim for better than scanning every report on each query.
Example
t = RoadwaySpeedTracker()
a = t.add(0, 10, 60)
b = t.add(5, 15, 30)
t.query(3) -> 60.0 # only a covers 3
t.query(5) -> 45.0 # (60 + 30) / 2
t.query(10) -> 30.0 # a ends at 10, which it does not cover
t.query(15) -> 0.0 # nothing covers 15
t.remove(a)
t.query(5) -> 30.0
Clarifying questions
- Can speeds be fractional, and how exact must the average be after many adds and removes?
- Is
endexclusive, soquery(end)is not covered? - What should
removeraise for an unknown or already removed id? - Must
addreturn ids that are never reused?
What a strong answer covers
- Turns each report into two range updates: add
speedand1to every position in[start, end); a query is the ratio of the two sums at one position. - Since coordinates reach
10^9and calls arrive online, uses a dynamic segment tree (nodes created on demand) or a Fenwick-style difference structure over a sparse map, givingO(log C)per call. - Stores each report by id so
removeapplies the opposite update, and checks for unknown ids. - Mentions floating-point drift from repeated add and remove, and avoids it by keeping exact sums when speeds are integers.
- Tests the example, removing every report, nested and touching segments, and a query at
10^9.
Follow-ups
- Return the length-weighted average over a range of positions.
- Reports expire after a time window.
- Return the maximum speed at a position instead of the mean.
Where it's asked
What it tests
segment tree · range update · data structure design
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.
