L1 · FoundationsText basics~2 min · 4 tests

Count Uppercase and Lowercase Letters

Count uppercase and lowercase letters in a string with isupper() and islower(), ignoring digits and symbols. A beginner Python loop, run live.

The problem

Return a tuple (uppercase_count, lowercase_count) for the letters in s. Ignore digits, spaces and symbols.

Examples

  1. Example 1

    Input

    case_counts('Hello World')

    Expected output

    (2, 8)
  2. Example 2

    Input

    case_counts('PYTHON')

    Expected output

    (6, 0)

+ 2 hidden tests on Submit.

Edge cases to ask about

  • Digits and symbols
  • Empty

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · case_counts
    ⌘/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
    One loop, two countersO(n)O(1)
    Walkthrough of the optimal approach (try it yourself first)

    Loop once and increment the right counter. isupper()/islower() return False for digits and symbols, so they are ignored automatically.

    Complexity: O(n) time, O(1) space. One pass, two integer counters.

    Reveal the reference solution
    def case_counts(s):
        upper = lower = 0
        for ch in s:
            if ch.isupper():
                upper += 1
            elif ch.islower():
                lower += 1
        return upper, lower

    Follow-ups interviewers ask

    • Swap the case of every letter without swapcase().

    Frequently asked interview questions

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

    What is the time complexity of Count Uppercase and Lowercase Letters in Python?

    The optimal solution runs in O(n) time and O(1) auxiliary space. One pass, two integer counters.

    What follow-up questions do interviewers ask about Count Uppercase and Lowercase Letters?

    Swap the case of every letter without swapcase().