L1 · FoundationsText basics~2 min · 4 tests

Count the Vowels in a String

Count the vowels in a Python string, upper and lower case, with a loop and a set lookup in O(n). A beginner interview warm-up you can run in the browser.

The problem

Return how many vowels (a e i o u, either case) are in s.

Examples

  1. Example 1

    Input

    count_vowels('Python Interview')

    Expected output

    5
  2. Example 2

    Input

    count_vowels('rhythm')

    Expected output

    0

+ 2 hidden tests on Submit.

Edge cases to ask about

  • Empty string
  • Upper case
  • No vowels

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · count_vowels
    ⌘/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
    Loop + set membershipO(n)O(1)The vowel set has a fixed size.
    Walkthrough of the optimal approach (try it yourself first)

    Walk the string and count characters found in a vowel set. sum(1 for ch in s if ch in vowels) does it in one line without building a list. Lower-casing first (s.lower()) works too but creates a copy.

    Complexity: O(n) time, O(1) space. Each character is checked once against a fixed 10-letter set.

    Reveal the reference solution
    def count_vowels(s):
        vowels = set("aeiouAEIOU")
        return sum(1 for ch in s if ch in vowels)

    Follow-ups interviewers ask

    • Return counts per vowel.

    Frequently asked interview questions

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

    What is the time complexity of Count the Vowels in a String in Python?

    The optimal solution runs in O(n) time and O(1) auxiliary space. Each character is checked once against a fixed 10-letter set.

    What follow-up questions do interviewers ask about Count the Vowels in a String?

    Return counts per vowel.