When can viewers start watching a just-uploaded video?
Question
A video site runs these jobs on each upload. Time starts when the upload finishes. A job starts as soon as every job it needs is done, and there are enough workers for all of them at once.
Viewers can start once one quality is packaged: its video and the audio, cut into segments and listed in the playlist the player reads.
| job | takes | needs |
|---|---|---|
| inspect | 2 s | the upload |
| audio | 6 s | inspect |
| 360p | 8 s | inspect |
| 720p | 20 s | inspect |
| 1080p | 40 s | inspect |
| package 360p | 2 s | 360p, audio |
| package 720p | 2 s | 720p, audio |
| package 1080p | 2 s | 1080p, audio |
- When can viewers start? When is the whole job done?
- A teammate makes the 360p encode twice as fast. What changes?
Show hints (3)
- "TWO SIMPLE RULES"
A chain adds up its times. A job that needs several jobs waits for the slowest one. Work out each finish time from the top.
- "WHAT VIEWERS NEED"
Viewers don't need 1080p to start. Which finish time matters for them?
- "THE SLOWER INPUT"
Package 360p waits for both 360p and audio. If the 360p encode takes 4 s instead of 8, which one does the package wait for?
Show checked explanation
Viewers can start at 12 s, when 360p is packaged. The whole job ends at 44 s. Each job ends at its own time plus the latest end among the jobs it needs. A 360p encode twice as fast saves only 2 s: its package then waits for audio, which ends at 8 s.
jobs = { # name: (seconds, jobs it needs)
"inspect": (2, []),
"audio": (6, ["inspect"]),
"360p": (8, ["inspect"]),
"720p": (20, ["inspect"]),
"1080p": (40, ["inspect"]),
"package 360p": (2, ["360p", "audio"]),
"package 720p": (2, ["720p", "audio"]),
"package 1080p": (2, ["1080p", "audio"]),
}
done = {}
for name, (seconds, needs) in jobs.items(): # each job after what it needs
done[name] = seconds + max((done[n] for n in needs), default=0)
for name in ["package 360p", "package 720p", "package 1080p"]:
print(name, "ready at", done[name], "s")
print("whole job done at", max(done.values()), "s")package 360p ready at 12 s package 720p ready at 24 s package 1080p ready at 44 s whole job done at 44 s
Code run on CPython 3.12 against the examples above. Checked by an editor other than the author.
Walkthrough
Go down the table. Each job ends at its own time plus the latest end among its inputs:
| job | starts | finishes |
|---|---|---|
| inspect | 0 | 2 |
| audio | 2 | 8 |
| 360p | 2 | 10 |
| 720p | 2 | 22 |
| 1080p | 2 | 42 |
| package 360p | 10 | 12 |
| package 720p | 22 | 24 |
| package 1080p | 42 | 44 |
The jobs form a dependency graph with no loops, often called a DAG. The chain with the longest total time is the critical path: nothing can finish sooner. For the whole job, it is inspect, 1080p, package 1080p: 44 s.
Viewers don't need the whole job. They need one quality with sound. Its longest chain is inspect, 360p, package 360p: 12 s, which is 32 s before the job is done. So don't keep one "ready" flag for the video. Track each quality, and add it to the playlist once its package is stored. 720p is listed at 24 s and 1080p at 44 s.
Now make the 360p encode twice as fast. It ends at 6 s, but audio runs until 8 s, and the package needs both. So the package ends at 10 s: 2 s saved, not 4. Speeding up one job helps only until another chain becomes the longest.
Common mistakes
- Adding up every step on the path. Inspect, audio, 360p and package make 18 s, but audio and 360p run side by side, so it is 12 s.
- Listing a quality before its package is stored. The player asks for segments that don't exist yet and stalls. List it only when every segment can be downloaded.
- Starting the next job on every "done" message. A retried worker can send "done" twice, so the package runs twice. Key each job by video and step, and start it only once.
Follow-up questions
- "You have one worker, not one per job. Now what?" Jobs run one at a time, so the order decides. Run inspect, audio, 360p and package 360p first, and viewers start at 18 s. Everything ends at 82 s, the sum of all jobs. With limited workers, the critical path is only a lower bound.
- "The 720p encode fails at 22 s. What do viewers see?" Nothing breaks. 360p is already listed, so they keep watching it. Retry 720p after a short pause, say 2 s. It ends at 44 s and joins the playlist at 46 s. If it keeps failing, publish without 720p and alert someone.
- "The video is two hours long, and 360p alone takes 20 minutes. Now what?" Split the video into short chunks and encode them on many workers. HLS, a common streaming format, has a playlist type that only grows at the end, so finished chunks can be added in order. Encoding must stay ahead of playback.
What a strong answer covers
Applies the rule: own time plus the latest end among the inputs. Finds 12 s for the first quality and 44 s for the whole job. Tracks readiness per quality and sees that audio limits a faster 360p encode.
Open in interactive practice deck →