L2 · Working engineerStrings~6 min · 5 tests#16

First Non-Repeating Character

Find the first character that appears exactly once in a Python string using a frequency count — two passes, O(n). Run tests and measure it live.

The problem

Return the first character in s that appears exactly once. Return None if every character repeats.

Examples

  1. Example 1

    Input

    first_unique_char('aabbcdde')

    Expected output

    'c'
  2. Example 2

    Input

    first_unique_char('swiss')

    Expected output

    'w'

+ 3 hidden tests on Submit — none unique, empty.

Edge cases to ask about

  • Empty string
  • All repeating
  • Single character

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · first_unique_char
    ⌘/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
    s.count() per characterO(n²)O(1)Recounts the whole string for each character.
    bestCount, then scanO(n)O(k)One pass to count, one pass to find the first count of 1. k = alphabet size.
    Walkthrough of the optimal approach (try it yourself first)

    Two passes. First build a frequency map. Then walk the string again and return the first character whose count is 1 — walking the string, not the dict, is what guarantees "first".

    Space is O(k) where k is the number of distinct characters — for lowercase English that is a constant 26, which is why some interviewers call it O(1).

    Complexity: O(n) time, O(k) space. Two linear passes; the count dict holds at most one entry per distinct character (k ≤ alphabet size).

    Reveal the reference solution
    def first_unique_char(s):
        counts = {}
        for ch in s:
            counts[ch] = counts.get(ch, 0) + 1
        for ch in s:
            if counts[ch] == 1:
                return ch
        return None

    The brute force, for comparison

    def first_unique_char(s):
        for ch in s:
            if s.count(ch) == 1:
                return ch
        return None

    Follow-ups interviewers ask

    • Return the index instead.
    • Characters arrive as a stream — answer after every character.

    Frequently asked interview questions

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

    What is the time complexity of First Non-Repeating Character in Python?

    The optimal solution runs in O(n) time and O(k) auxiliary space. Two linear passes; the count dict holds at most one entry per distinct character (k ≤ alphabet size).

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

    s.count() per character: O(n²) time, O(1) space. Recounts the whole string for each character. Count, then scan: O(n) time, O(k) space. One pass to count, one pass to find the first count of 1. k = alphabet size.

    What follow-up questions do interviewers ask about First Non-Repeating Character?

    Return the index instead. Characters arrive as a stream — answer after every character.