If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

Verifying an algorithm

AP.CSP: CRD‑2 (EU), CRD‑2.I (LO), CRD‑2.I.1 (EK), CRD‑2.I.5 (EK), CRD‑2.J (LO), CRD‑2.J.1 (EK), CRD‑2.J.2 (EK), CRD‑2.J.3 (EK)

Problem

The goal of the procedure longestWord is to return the longest word in a given list of words. For example, if it's given the list ["superhero", "captain", "marvel"], it should return "superhero" as the longest word.
PROCEDURE longestWord(words) {
    maxWordLen ← 0
    maxWord ← ""
    FOR word IN words {
        IF (LENGTH(word) > maxWordLen) {
           maxWord ← word
           maxWordLen ← LENGTH(word)
           RETURN maxWord
        }
    }
    RETURN maxWord
}
A programmer tests the procedure with various inputs and finds that there are multiple cases where it does not produce the expected output.
Which calls to longestWord() do not actually return the longest word?
👁️Note that there are 2 answers to this question.
Choose 2 answers:
Choose 2 answers:
Stuck?
Stuck?