L2 · Working engineerStrings~4 min · 5 tests#22

Find the Longest Word in a Sentence

Find the longest word in a sentence with split() and a single pass, returning the first one on ties. Practise max() with key= in Python.

The problem

Return the longest word in sentence (words are separated by whitespace). On a tie, return the word that appears first. Return "" for an empty sentence.

Examples

  1. Example 1

    Input

    longest_word('Python is powerful programming language')

    Expected output

    'programming'
  2. Example 2

    Input

    longest_word('a bb cc')

    Expected output

    'bb'

+ 3 hidden tests on Submit.

Edge cases to ask about

  • Empty sentence
  • Multiple spaces
  • Ties

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · longest_word
    ⌘/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 measure your code against the optimal one at growing input sizes.

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

    Pick both to reveal the answer.

    Measure it

    Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.

    From brute force to optimal

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

    ApproachTimeSpaceIdea
    Sort words by lengthO(w log w)O(w)More work than needed.
    bestSingle pass / max(key=len)O(n)O(w)max() returns the first maximal element, which handles ties.
    Walkthrough of the optimal approach (try it yourself first)

    sentence.split() (no argument) handles multiple spaces and strips the ends. Then track the longest word, replacing it only when a word is strictly longer — that keeps the first one on ties.

    max(sentence.split(), key=len, default="") is the one-liner; max also returns the first maximal item.

    Complexity: O(n) time, O(n) space. split() walks the sentence once and stores the words; the comparison loop is linear in the number of words.

    Reveal the reference solution
    def longest_word(sentence):
        best = ""
        for word in sentence.split():
            if len(word) > len(best):
                best = word
        return best

    Follow-ups interviewers ask

    • Strip punctuation first.
    • Return all words tied for longest.

    Frequently asked interview questions

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

    What is the time complexity of Find the Longest Word in a Sentence in Python?

    The optimal solution runs in O(n) time and O(n) auxiliary space. split() walks the sentence once and stores the words; the comparison loop is linear in the number of words.

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

    Sort words by length: O(w log w) time, O(w) space. More work than needed. Single pass / max(key=len): O(n) time, O(w) space. max() returns the first maximal element, which handles ties.

    What follow-up questions do interviewers ask about Find the Longest Word in a Sentence?

    Strip punctuation first. Return all words tied for longest.