Three easy problems the way a media or streaming interview frames them: two playlists merged, a shuffle that skips close repeats, and the busiest minute of a live stream. A 45-minute round with a plan.
A media or streaming company plays music, video and live events for a very large audience. Its coding interviews use the same data structures as any other company, but the stories are about queues, histories and crowds. The interviewer listens for four habits.
The warm-up round tests these habits on three easy problems. The interviewer often opens with "What would you ask?" Have your questions ready.
Is each list sorted? Does a track in both lists play once? What does "recently" count? Is the leave minute included?
This is the first of three rounds in the media and streaming set. The core and stretch rounds that follow use the same habits on harder problems. They cover watch time in a window, a shelf that remembers, and a video quality that fits a budget.
Exercise · Predict the output: A skipped track never played. A shuffle plays tracks from a list. A track is skipped when the same track is among the last 2 tracks that really played. What does this print: the tracks that played, in order?
tracks = [1, 2, 1, 3, 1, 2]
gap = 2
played = []
last = {}
for track in tracks:
if track in last and len(played) - last[track] <= gap:
continue
last[track] = len(played)
played.append(track)
print(*played)Each problem names its move in the statement. Read the signal, then use the idea the Academy already taught you.
| Signal in the statement | The move | Academy lesson |
|---|---|---|
| two sorted lists, a track in both plays once, stop at a limit | two cursors that take the smaller front, advance both on a tie, and stop at the limit | Merging sorted sequences |
| skip a track heard in the last few that played | a map from each track to the place where it last played | Seen-set and complement |
| how many at the same time, from joins and leaves | sort the joins and the leaves apart and sweep, with leaves before joins at one minute | Intervals |
What is new here is the frame, not the idea. A tie between the two fronts means the same track is in both lists, so both cursors move. The map stores the place among tracks that played, never the place in the queue, and a skipped track leaves it alone. And at one minute, the viewers who leave are removed before the viewers who join are added.
The merge shows the habit of stopping early. The loop ends when the queue holds the limit, even if both lists still have tracks.
Exercise · Predict the output: Merge and stop at the limit. Two playlists are sorted by rank, and a rank in both plays once. The queue stops after 4 tracks. What does this print?
a = [1, 4, 7]
b = [4, 5, 9]
limit = 4
i = 0
j = 0
queue = []
while len(queue) < limit and (i < len(a) or j < len(b)):
if j == len(b) or (i < len(a) and a[i] < b[j]):
queue.append(a[i])
i += 1
elif i == len(a) or b[j] < a[i]:
queue.append(b[j])
j += 1
else:
queue.append(a[i])
i += 1
j += 1
print(*queue)The sweep is where a one-character slip hides. A viewer who leaves at the minute another viewer joins must be removed first.
Exercise · Find the bug: The viewer who leaves at the same minute. Viewer i watches from minute joins[i] up to, but not including, minute leaves[i]. This returns [peak, first minute of the peak]. It is right when no viewer leaves at the minute another joins, but wrong otherwise. Which line is wrong?
def peak_viewers(joins, leaves):
starts = sorted(joins)
ends = sorted(leaves)
watching = 0
peak = 0
first = 0
j = 0
for i in range(len(starts)):
while ends[j] < starts[i]:
watching -= 1
j += 1
watching += 1
if watching > peak:
peak = watching
first = starts[i]
return [peak, first]The gap counts tracks that really played: a skipped track leaves the history alone.
A viewer who leaves at minute t is gone at t: remove the leaves before adding the joins at the same minute.
All three problems are easy, and together they make one warm-up round. Each time box is close to what an interviewer allows for an easy problem, with time to explain.
Solve them in this order. The merge is two cursors, the shuffle adds a map, and the sweep adds two sorted lists read together.
A warm-up problem rarely ends at the first answer. Once your code works, the interviewer changes one rule and asks what changes in your code. Prepare one follow-up for each problem.
Treat the three problems as one interview: 45 minutes in all, with a timer. Work out loud, as if someone is listening.
If a problem takes longer than its box, finish it anyway, but note the overrun. In a real round the interviewer would have moved on, so the next attempt should be faster.
Checkpoint · Media and streaming warm-up round
Q1/5 Two playlists hold ranks 1, 4, 7 and 4, 5, 9. A rank in both plays once. What are the first 4 tracks of the queue?
Answer: B
Rank 4 is in both lists and plays once, so the queue is 1, 4, 5 and 7. The loop then stops at the limit of 4.
Q2/5 Tracks 1, 2, 1, 3, 1, 2 are shuffled, and a track is skipped when it is among the last 2 that played. Which tracks play?
Answer: B
The second 1 is skipped. The next 1 follows tracks 2 and 3, so it plays, and so does the last 2. Only the tracks that played count.
Q3/5 A track is skipped and never played. What does that do to the clock for later tracks?
Answer: B
A skipped track was never heard, so it leaves the history unchanged. Counting it would make later tracks skip too soon.
Q4/5 One viewer watches from minute 0 to minute 5 and another from minute 5 to minute 9. What are the peak and its first minute?
Answer: B
The first viewer is gone when minute 5 begins, so nobody watches at the same time as anyone else. The peak is 1, first reached at minute 0.
Q5/5 Why merge two sorted playlists with two cursors and not sort them together?
Answer: B
The lists are already sorted, so the merge needs only a single pass and can stop early. Sorting repeats work that is already done.
Review cards from this lesson.
A few questions, then explain the idea back in your own words. Your tutor marks it. Quiz and explain back.