L1 · FoundationsText basics~4 min · 6 tests

Longest Common Prefix of Strings

Find the longest prefix shared by all strings in a list by comparing character columns, stopping at the first mismatch. O(total characters), tested live.

The problem

Return the longest string that is a prefix of every word in words. Return "" if there is none or the list is empty.

Examples

  1. Example 1

    Input

    common_prefix(['flower', 'flow', 'flight'])

    Expected output

    'fl'
  2. Example 2

    Input

    common_prefix(['dog', 'racecar', 'car'])

    Expected output

    ''

+ 4 hidden tests on Submit.

Edge cases to ask about

  • Empty list
  • Single word
  • An empty string in the list

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · common_prefix
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and read why.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    Column by columnO(S)O(1)S = total characters; stops at the first mismatch.
    bestSort, compare first and lastO(S log n)O(1)After sorting, only the extremes matter.
    Walkthrough of the optimal approach (try it yourself first)

    The prefix can be no longer than the shortest word, so walk its characters and check each position against every word. The first mismatch ends the prefix.

    os.path.commonprefix(words) does this in the standard library.

    Complexity: O(S) time, O(1) space. Each character position is compared across words at most once; S is the total number of characters.

    Reveal the reference solution
    def common_prefix(words):
        if not words:
            return ""
        shortest = min(words, key=len)
        for i, ch in enumerate(shortest):
            for w in words:
                if w[i] != ch:
                    return shortest[:i]
        return shortest

    Follow-ups interviewers ask

    • Solve it with a trie.

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Longest Common Prefix of Strings in Python?

    The optimal solution runs in O(S) time and O(1) auxiliary space. Each character position is compared across words at most once; S is the total number of characters.

    What is the brute-force approach, and how do you optimise it?

    Column by column: O(S) time, O(1) space. S = total characters; stops at the first mismatch. Sort, compare first and last: O(S log n) time, O(1) space. After sorting, only the extremes matter.

    What follow-up questions do interviewers ask about Longest Common Prefix of Strings?

    Solve it with a trie.