第 7 周 - 第 3 天 - 2026-09-09 WED - FULL - Top K HLD

YouTube Top K FULL: all-time and tumbling-window HLD

08:50-10:55 system design14:30-16:15 Trees x320:30-21:15 recall

今日目标

把 Top K 从 SQL ORDER BY 进化到 view-event stream, window aggregates, and materialized Top K view.

08:50-09:00Set target
定义 Top K contract: exact, max K=1k, windows, and tolerated staleness.
09:00-10:05Attempt then read
25-35 分钟闭卷设计 Top K,再读 requirements, API, all-time and tumbling-window HLD.
10:05-10:45Decision note
写 all-time counter, hourly aggregate row, query bottleneck, and materialized view option.
10:45-10:55Spoken close
英语复述 exact tumbling-window Top K, including why query latency forces precompute.
14:30-16:15NeetCode Trees
Exactly three contiguous Trees slots. Each slot is 30m solve + 5m pattern/invariant/bug/complexity evidence.
20:30-21:15Recall / Q&A / repair
20:30-21:15 Top K English recall outline: state the contract, HLD, and first bottleneck without notes.

Exact source links

Use original Hello Interview titles and current canonical anchors. Inline Bad/Good/Great headings without standalone live anchors link to their parent canonical section.

Original title / direct linkDispositionUse
Understanding the Problemrequired readingprecise Top K over YouTube views
Functional Requirementsrequired readingquery top K for supported windows
Non-Functional Requirementsrequired reading1 minute delay, precise results, tens of ms reads, massive events/videos
The Set Uprequired readingincremental design route
Planning the Approachrequired readingall-time first, then windows, then bottlenecks
Defining the Core Entitiesrequired readingVideo, View, Time Window
API or System Interfacerequired readingGET /views/top-k?window=...
High-Level Designrequired readingview event stream, counters, top-K service
1) Clients should be able to query the top K videos for all-time (up to a max of 1k results).required readingall-time counters and sorted index
2) Clients should be able to query tumbling windows of 1 {hour, day, month} and all-time (up to a max of 1k results).required readingwindow rows, group/sum/query bottleneck

DDIA decision cards

Only exact Ch11/Ch12/Ch13 sections that change today's design are assigned.

DDIA Ch12

维护物化视图

Question: Top K 是实时算还是维护物化视图?

维护 per-window materialized view,查询只读最近完成的窗口或 bounded-staleness snapshot。

DDIA Ch12

窗口的类型

Question: 1h/1d/1m 的窗口到底是什么?

面试先选 tumbling;如果要 sliding,再讨论增量和过期 decrement 的成本。

算法模块

Contiguous live NeetCode Trees order. No reviews. Three slots per day.

#Problem / modePatternInvariantBug riskTimeSpace
7Diameter of Binary Tree
NEW - 30m solve + 5m evidence
后序高度 + 全局答案经过节点的路径长度为 leftHeight + rightHeight。把节点数和边数混淆。O(n)O(h)
8Balanced Binary Tree
NEW - 30m solve + 5m evidence
后序剪枝任一子树高度差大于 1 即不平衡。发现 -1 后仍继续覆盖结果。O(n)O(h)
9Same Tree
NEW - 30m solve + 5m evidence
双树同步 DFS两个节点同时为空或值相同且左右子树相同。一个空一个非空未先判。O(n)O(h)

今日验收

OutputTop K first attempt, corrected HLD, window contract, and latency/bottleneck note.
Hard failPassive reading without a spoken answer; missing invariant/failure/recovery/metric; any day outside Week 7 touched.
RepairPick the weakest answer and redo it for 8 minutes with one new failure window.
Local study materials
Detailed lecture notes, audio, recall scripts, PDFs, Staff Q&A, and mock packs are archived locally and are intentionally not published on this site.