Fix a Broken Podcast Episode Search Against Five Behavior Rules
AmazonSoftware Engineer · Online AssessmentLast updated
Fix a broken podcast episode search function so it matches substrings case-insensitively, supports searching by title, host, or both, sorts results correctly, and excludes unpublished episodes.
You're given a podcast app's episode search function. It compiles and runs, but it returns wrong results. Find and fix the bugs so the search satisfies all five rules below, without changing the function's signature.
def search_episodes(episodes, query, field="all"):
results = []
for ep in episodes:
if field == "title":
match = query == ep["title"]
elif field == "host":
match = query == ep["host"]
else:
match = query == ep["title"] or query == ep["host"]
if match:
results.append(ep)
results.sort(key=lambda e: -e["popularity"])
return results
Each episode dict has episode_id (int), title (str), host (str), popularity (int), and published (bool).
The five rules
- Partial match — an episode matches when
queryappears anywhere inside the searched field, not only when it equals the whole field. - Field choice —
fieldis"title","host", or"all"(match title or host). - Case-insensitive — matching ignores letter case on both sides.
- Ordering — sort by
popularitydescending; break ties bytitleascending (case-insensitive), then byepisode_idascending. - Published only — an episode with
published=Falseis never returned, regardless of how well it matches.
Part 1 — Write the failing tests
Before touching the implementation, write tests that pin down each of the five rules, so each one fails against the code above and passes once it's fixed.
Part 2 — Fix the function
Rewrite search_episodes so all five rules hold, and your Part 1 tests pass.
Example
episodes = [
{episode_id: 1, title: "Deep Work Habits", host: "Rae Linton", popularity: 42, published: True},
{episode_id: 2, title: "Habits of Great Teams", host: "Marco Diaz", popularity: 77, published: True},
{episode_id: 3, title: "Cooking with Data", host: "Rae Linton", popularity: 77, published: True},
{episode_id: 4, title: "Unreleased Pilot", host: "Habits Habits",popularity: 99, published: False},
]
search_episodes(episodes, "habits", field="title")
-> episodes 2, 1 (in that order)
Hints
Hint 1
For each rule, build a tiny episode list where exactly one specific wrong behavior — exact-match instead of substring, case sensitivity, wrong field, wrong order, or including a draft — would make the test fail.
Hint 2
Substring matching and case-insensitivity are two separate bugs; fix them together with `query.lower() in field_value.lower()`.
Hint 3
A sort key can be a tuple: combine the negated popularity with the lowercase title and the episode id to get a fully deterministic order.
Hint 4
The published filter should run before matching, not after sorting, so it can't accidentally interact with tie-breaking.
Follow-up questions
- How would you change the design to support ranking by relevance instead of only by popularity?
- If the episode list lived in a database instead of a Python list, how would these five rules map to a SQL query or ORM filter?
- How would you add pagination without breaking the tie-break ordering across pages?
- What's the cheapest way to make the substring match tolerant of simple typos?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.