Rank vehicles by peer-to-peer update contribution

0
Sign in

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 most 10^3 chunk 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.

Reconstructed from the reported question; details may differ from what interviewers ask.

TeslaTechnical phone screenMediumLast asked Jul 2025

Sign in to see the worked answer

The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.