Rank vehicles by peer-to-peer update contribution
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Tesla's technical phone screen, last asked Jul 2025. It tests hash map, sorting and simulation.
A fleet of electric vehicles receives over-the-air software updates split into fixed-size chunks. Chunks come from the distribution server over cellular data, which is expensive. To save data, vehicles can also pass chunks to nearby vehicles over a peer-to-peer link.
You are given events, a list of successful transfers in the order they happened. Each event is [sender, chunks, receiver]:
-
sender: a vehicle ID, or the string"server". -
chunks: a space-separated string of chunk IDs sent in this transfer. -
receiver: the vehicle ID that received them (never"server").
Implement rank_vehicles(events) -> list[str].
A vehicle earns one point for each chunk it delivers to another vehicle that did not already hold that chunk. Duplicate deliveries earn nothing, and the server earns nothing. Return every vehicle that appears in any event (as sender or receiver), sorted by points from high to low; break ties by vehicle ID in ascending order.
Constraints
-
0 <= len(events) <= 10^5; each event lists at most10^3chunk tokens. - IDs are non-empty strings without spaces.
- A chunk may appear more than once in the same event; it is delivered once.
Example 1
events = [
["server", "a b c", "car7"],
["car7", "a b", "car2"],
["car7", "a c", "car9"],
["car2", "a b", "car9"],
["server", "c", "car2"],
]
car7 delivers 4 new chunks. car2 sends a b to car9, but car9 already has a, so car2 earns 1. car9 sends nothing. Output: ["car7", "car2", "car9"].
Example 2
events = [["server", "x", "bus"], ["server", "x", "ace"]]
Both vehicles have 0 points, so they are ordered by ID. Output: ["ace", "bus"].
Be ready to discuss: returning only the top k, keeping the ranking live as events stream in, and rejecting transfers of chunks the sender does not hold.
Clarifying questions
- Does a vehicle that already holds a chunk count it as held if it got it from the server?
- Should a transfer of a chunk the sender does not hold be rejected, ignored, or counted?
- Can a vehicle send to itself, and does that earn anything?
- Is
"server"excluded from the output? - With up to
10^8chunk tokens in total, is keeping every vehicle's chunk set in memory acceptable?
What a strong answer covers
- Keeps a set of held chunks per vehicle and a point count per vehicle.
- For each event, splits and de-duplicates the chunk tokens, credits the sender with each chunk the receiver lacked (unless the sender is the server), then adds them to the receiver.
- Records every vehicle seen as sender or receiver, including those with 0 points, and sorts by
(-points, id). -
O(total tokens)for the events plusO(V log V)for the sort. - Tests both examples, duplicate tokens in one event, and repeated deliveries of the same chunk.
Follow-ups
- Return only the top
kvehicles. - Keep the ranking live as events stream in.
- Reject transfers of chunks the sender does not hold.
Where it's asked
- Tesla · Technical phone screen · Last asked Jul 202545 min · 10 to 20 minutes on your background, then one compact coding, debugging or design task in CoderPad that must run
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.
