The Sequel Spotter

An easy Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.

Domain
Python
Difficulty
easy
Seniority
L3
Asked in
Meta

Problem

We're deduping a media catalog where each sequel was named by extending an existing title, so a sequel's name begins with a shorter title already in the catalog. Return the titles that extend another title from the list, preserving the original order.

Spot the sequels hiding in the catalog.

Example

Input:

titles = ["GOT","Troy","Batman","Batman Returns","GOT Part 2"]

Output:

["Batman Returns","GOT Part 2"]

Worked solution and explanation

The self-match trap

Underneath the movie-catalog framing this is a proper-prefix scan with a self-exclusion trap. Every string startswith() itself, so the moment you test a title against the whole list you match it to itself and flag it as its own sequel. Anyone can write the two loops; what separates candidates is remembering to exclude a title from prefixing itself. Miss that and every single title comes back as a sequel: 100 percent false positives.


Break down the requirements

Step 1: Every entry is a candidate prefix

There is no separate list of 'originals'. Any title in the input can play the shorter original, so for each title you test it against all the others in the same list.

Step 2: Make the prefix proper, not just any prefix

startswith() is necessary but not sufficient: 'Batman'.startswith('Batman') is True, so you must exclude a title from matching itself. Two guards do this identically here: len(other) < len(title) and other != title. When a prefix actually matches, equal length forces the two strings to be equal, so both guards accept exactly the same pairs. Prefer the length form: it names the real requirement, a shorter original, and it drops duplicate titles with no special case.

Step 3: Keep the survivors in input order

Collect the titles that have at least one qualifying shorter prefix, walking front to back so the output order matches the input order.


The solution

Proper-prefix scan with short-circuit
def find_sequels(titles: list[str]) -> list[str]:
    return [
        title
        for title in titles
        if any(len(other) < len(title) and title.startswith(other)
               for other in titles)
    ]

any() stops at the first shorter title that prefixes this one; the length guard is what excludes the title from prefixing itself.


Common follow-up questions