Three hard problems the way a media or streaming interview frames them: steady clips in a brightness track, the kth cheapest bundle across shelves, and video quality under a data budget. A 125-minute round with a plan.
The stretch round has hard problems about huge catalogues and long streams of data. The stories are about shelves, quality levels and brightness, but each one is a classic that you must name and cost. The interviewer listens for four habits.
The stretch round has three hard problems: steady clips in a brightness track, the kth cheapest bundle across shelves, and video quality under a data budget. Together they take over two hours.
This is the third of three rounds in the media and streaming set. Do the core round first: the design-round habit of stating costs carries over.
Exercise · Predict the output: The score of one session. Each of 3 segments has two options with a size and a quality. The session picks option 1, 1 and 0. The score is the total quality minus a penalty of 30 for every pair of neighbouring segments whose options differ, and the total size must be at most 10. What does this print: the total size, then the score?
sizes = [[2, 4], [2, 4], [2, 4]]
quality = [[100, 160], [100, 160], [100, 160]]
choice = [1, 1, 0]
penalty = 30
size = 0
score = 0
for i in range(3):
size += sizes[i][choice[i]]
score += quality[i][choice[i]]
if i > 0 and choice[i] != choice[i - 1]:
score -= penalty
print(size, score)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 |
|---|---|---|
| every stretch where the largest minus the smallest stays small | a window that grows on the right and shrinks on the left, with two queues for the extremes | Fixed and variable windows |
| one pick from every shelf, the kth smallest total, too many bundles to list | merge the sums shelf by shelf and keep only the k smallest | Top k and k-way merge |
| one choice for each segment, a total size budget, a penalty between neighbours | a table over the budget used and the last choice | Knapsack: take it or leave it |
What is new here is the frame, not the idea. A steady window counts every clip that ends at the current frame, so the answer adds the window length each time. The bundle totals are merged one shelf at a time, and equal totals stay as separate entries. And the knapsack has groups, so the table takes exactly one option from each segment.
The bundle problem is where the order of the steps matters. Keep the k smallest sums after each shelf, and never merge duplicates away.
Exercise · Put it in order: The kth cheapest bundle. Arrange the lines into a function that returns the k-th smallest total of one title from the first shelf and one from the second. Two bundles with equal totals are different bundles.
def kth_cheapest(first, second, k):
sums = []
for a in first:
for b in second:
sums.append(a + b)
sums.sort()
return sums[k - 1]
sums = sorted(set(sums))Equal totals are different bundles: dropping repeats moves the answer from 5 to 6.
The window problem has a counting mistake that looks like a small slip. The code below uses two simple scans for the largest and smallest, so that the counting is easy to see.
Exercise · Find the bug: Clips that end at this frame. A clip is steady when its largest brightness minus its smallest is at most the tolerance. The left end moves right until the window is steady, and each frame adds the clips that end there. It is wrong even for one frame. Which line is wrong?
def spread(brightness, left, right):
low = brightness[left]
high = brightness[left]
for i in range(left, right + 1):
low = min(low, brightness[i])
high = max(high, brightness[i])
return high - low
def count_steady_clips(brightness, tolerance):
left = 0
total = 0
for right in range(len(brightness)):
while spread(brightness, left, right) > tolerance:
left += 1
total += right - left
return totalEach frame adds the clips that end there: right minus left plus one.
All three problems are hard, and together they make one stretch round. Each time box is close to what an interviewer allows for a hard problem when they want the model, the code and the cost.
Solve them in this order. The first uses a window, the second merges sums, and the third needs a table with a state for the last choice.
Hard problems come with a follow-up that tests whether you understood the model. Prepare one for each problem.
Treat the three problems as one long session: 125 minutes in all, with a timer, and take a short break between problems. Work out loud and name the cost before you run.
If a problem takes longer than its box, finish it anyway, but note the overrun.
Checkpoint · Media and streaming stretch round
Q1/5 A session picks options with sizes 4, 4 and 2 and qualities 160, 160 and 100. The options change once, with a penalty of 30. What is the score?
Answer: B
The qualities add to 420, and one change costs 30, so the score is 390. The total size, 10, must also fit the budget.
Q2/5 Shelves hold fees 1, 2 and 3, 4. Every pair makes a bundle. What is the third smallest total?
Answer: A
The totals are 4, 5, 5 and 6, so the third place is 5. Removing the repeated 5 would give 6.
Q3/5 Brightness is 1, 3, 2, 8 and the tolerance is 2. How many steady clips are there?
Answer: B
The four single frames are steady, and so are the clips 1 3, 3 2 and 1 3 2. That makes 7. Counting right - left instead of right - left + 1 gives 3.
Q4/5 Why does the merge keep only the k smallest sums after each shelf?
Answer: A
Adding a fee only makes a sum larger, so a sum outside the k smallest now cannot return to the k smallest later.
Q5/5 What must the table of the quality problem remember besides the budget used?
Answer: A
The penalty depends on whether this choice differs from the one before it, so the last choice is part of 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.