Three medium problems the way a media or streaming interview frames them: watch time in a date window, the smallest valid viewing order, and a continue-watching shelf as a design round. An 80-minute round with a plan.
The warm-up round of this set checked sorted input, histories of what played, half-open sessions and stopping early. The core round keeps those habits and adds medium problems on data that is large, sparse and stateful. The interviewer listens for four new habits.
The core round has three medium problems: watch time in a date window, the smallest valid viewing order, and a continue-watching shelf. The last one is a design round, where the answers come from a list of commands.
This is the second of three rounds in the media and streaming set. Warm up first with the warm-up round if merging, histories and sweeps still feel slow.
Exercise · Predict the output: The smallest ready episode. Episode a must come before episode b for every pair [a, b]. An episode is ready when all the episodes before it have been watched, and the next one watched is always the ready episode with the lowest number. What does this print?
n = 5
requires = [[3, 1], [3, 0], [1, 4]]
indegree = [0] * n
for a, b in requires:
indegree[b] += 1
done = [False] * n
order = []
for _ in range(n):
pick = min(i for i in range(n) if not done[i] and indegree[i] == 0)
done[pick] = True
order.append(pick)
for a, b in requires:
if a == pick:
indegree[b] -= 1
print(*order)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 |
|---|---|---|
| a total over a range of days, sparse events, many queries | sort the events, take prefix sums over them, and find both ends of each window by binary search | Prefix sums in 1D and 2D and Lower and upper bound |
| episode A before episode B, the smallest valid order | count the episodes each one waits for, and keep the ready ones in a heap | Topological sort |
| a shelf of fixed size, the title played longest ago leaves first | a map from title to a node, and a doubly linked list in play order | Doubly linked lists and the LRU cache |
What is new here is the frame, not the idea. The events are sparse, so the prefix sums go over the sorted events and not over every day. A window ends at the first event after its last day.
The heap replaces the queue, so ties between ready episodes are settled by number. And the shelf has a command that must read without touching.
The window problem is where the boundary decides the answer. The last day of a window is included, so the search for its end must look past it.
Exercise · Find the bug: The last day of the window. The days of one profile are sorted, and prefix sums hold the minutes watched. This returns the minutes watched from day start to day stop, both days included. It is right when no event falls on the last day, but wrong otherwise. Which line is wrong?
def first_at_least(days, x):
lo, hi = 0, len(days)
while lo < hi:
mid = (lo + hi) // 2
if days[mid] < x:
lo = mid + 1
else:
hi = mid
return lo
def watch_total(days, minutes, start, stop):
prefix = [0]
for m in minutes:
prefix.append(prefix[-1] + m)
lo = first_at_least(days, start)
hi = first_at_least(days, stop)
return prefix[hi] - prefix[lo]A closed window ends at the first day after it: search for the last day plus one.
The shelf is the design round. Peek and rank must give answers without changing the order, and a peek that moves a title is the mistake that hides longest.
Exercise · Break it: A peek that touches. A shelf holds the most recent titles. Play saves a position and makes the title the most recent, peek returns the saved position, and rank k returns the k-th most recent title. A peek must not change the order. Find an input where this one does.
def run_shelf(capacity, commands, titles, positions):
shelf = []
saved = {}
answers = []
for i in range(len(commands)):
if commands[i] == "play":
if titles[i] in shelf:
shelf.remove(titles[i])
shelf.insert(0, titles[i])
saved[titles[i]] = positions[i]
if len(shelf) > capacity:
del saved[shelf.pop()]
elif commands[i] == "peek":
if titles[i] in saved:
shelf.remove(titles[i])
shelf.insert(0, titles[i])
answers.append(saved.get(titles[i], -1))
else:
k = titles[i]
answers.append(shelf[k - 1] if k <= len(shelf) else -1)
return answersPeek and rank only read the shelf: a peek that moves a title changes every later rank.
All three problems are medium, and together they make one core round. Each time box is close to what an interviewer allows for a medium problem. The last problem is a design round: you answer a list of commands, and each command has a cost.
Solve them in this order. The first sorts and searches, the second adds a heap to a graph order, and the third combines a map with a linked list.
Core problems invite a bigger follow-up. The interviewer changes one rule and asks whether your idea still holds. Prepare one for each problem.
Treat the three problems as one session: 80 minutes in all, with a timer. Work out loud, and say the cost of each command or step as you go.
If a problem takes longer than its box, finish it anyway, but note the overrun.
Checkpoint · Media and streaming core round
Q1/5 The days of one profile are 2, 5 and 9, with 10, 20 and 30 minutes. How many minutes are watched from day 2 to day 5, both included?
Answer: B
Days 2 and 5 are both in the window, so the total is 10 + 20 = 30. Searching for day 5 itself leaves day 5 out and gives 10.
Q2/5 Episode 3 must come before episodes 0 and 1, and episode 1 before episode 4. Episodes 2 has no rule. What is the smallest valid order?
Answer: B
Episodes 2 and 3 are ready first, and 2 is lower. After 3, episodes 0 and 1 are ready and 0 is lower. A plain queue would put 1 before 0.
Q3/5 Some episodes require each other in a circle. What does the viewing order return?
Answer: B
An episode in a circle never becomes ready, so the order cannot include all episodes. The problem asks for an empty list in that case.
Q4/5 A shelf holds titles 2 and 1, with 2 the most recent. A peek at title 1 returns its position. Which title is rank 1 afterwards?
Answer: B
Peek reads and does not touch. Title 2 stays the most recent, so rank 1 is still title 2.
Q5/5 What makes the continue-watching shelf a design round?
Answer: A
A design round gives you commands to support and not one function. Say each command and its cost, and say which commands must not change the state.
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.