Skip to the workspace
Pebellslearn by building
Trees, Heaps & Tries  ›  Capstone Project

Capstone — Discovery Feed

35 minA tree, a trie and a heap working as one pipeline

dsa / Capstone — Discovery Feed

Explore each scene at your own pace.

01 · Pin the spec down before writing code

Pin the spec down before writing code

Capstone — Discovery Feed

Definition
The discovery feed combines a genre tree to choose which genres count, a trie to find titles matching a typed prefix, and a heap to pick the top k by plays, so each stage narrows what the next must handle.
In plain words
It is three filters in a row: first which shelves of the shop you may browse, then which books start with what you typed, then the few most popular among those. Each step hands the next fewer items to sort.

In short: the discovery tab is where this section's three structures meet. A listener who loves Rock types "ne", and the feed should show the best few Rock tracks — Punk and Metal included — whose titles start with "ne", most-played first. Three questions, three structures: the genre tree says which genres count, a trie says which titles match, and a heap picks the top k. Each stage hands the next fewer candidates, so the heap — the most expensive step per item — only ever sorts the tracks that could possibly win.

Three boxes in a row — genre tree, title trie, top-k heap — each narrowing the candidate count from all genres, to allowed titles, to k results

Pin the spec down before writing code

discovery_feed(genres, tracks, favorite, prefix, k) takes:

  • genres — the genre tree as a level-order list, None for a missing child
  • tracks — [title, genre, plays] triples; titles are unique and lowercase
  • favorite — a genre; the feed draws from it and every genre beneath it
  • prefix — what's been typed so far; "" matches everything
  • k — how many titles to return

It returns up to k titles, most plays first, ties broken alphabetically. A favorite that isn't in the tree gives [], and a track whose genre isn't in the tree is never picked. Pinning ties down isn't pedantry: two listeners with the same taste must see the same feed, and a test can only check an answer that has exactly one right value.

Scene 1 of 9
Project context and further resources
You're building a piece ofTunebox — discovery
This piece — discovery_feed(): Builds a personal feed from genres, charts and search.
Scenario A listener who loves Rock opens Tunebox's discovery tab and types "ne". The feed shows the best Rock, Punk and Metal tracks starting with "ne", most-played first.
discovery_feed.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
discovery_feed(genres, tracks, favorite, prefix, k) → listUp to k titles from the favorite genre's subtree that start with prefix, most-played first.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.