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

Remove Duplicate Characters from a String

Remove repeated characters from a Python string, keeping first occurrences in order, with a seen-set and join in O(n). Run hidden tests in your browser.

The problem

Return s with every repeated character removed, keeping the first occurrence of each character in order.

Examples

  1. Example 1

    Input

    remove_duplicate_chars('programming')

    Expected output

    'progamin'
  2. Example 2

    Input

    remove_duplicate_chars('aabbcc')

    Expected output

    'abc'

+ 3 hidden tests on Submit — space is a character.

Edge cases to ask about

  • Empty string
  • No duplicates
  • Spaces

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · remove_duplicate_chars
    ⌘/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
    `ch not in out` on a stringO(n·k)O(k)Each check scans the result.
    bestSeen set + list + joinO(n)O(k)Or simply ''.join(dict.fromkeys(s)).
    Walkthrough of the optimal approach (try it yourself first)

    Same pattern as question 2: a seen set for O(1) membership and a list to keep order, then one "".join().

    "".join(dict.fromkeys(s)) works because dicts preserve insertion order.

    Complexity: O(n) time, O(k) space. One pass with O(1) set checks; extra memory is one entry per distinct character.

    Reveal the reference solution
    def remove_duplicate_chars(s):
        seen = set()
        out = []
        for ch in s:
            if ch not in seen:
                seen.add(ch)
                out.append(ch)
        return "".join(out)

    The brute force, for comparison

    def remove_duplicate_chars(s):
        out = ""
        for ch in s:
            if ch not in out:
                out += ch
        return out

    Follow-ups interviewers ask

    • Remove only adjacent duplicates ('aabba' → 'aba').
    • Case-insensitive version.

    Frequently asked interview questions

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

    What is the time complexity of Remove Duplicate Characters from a String in Python?

    The optimal solution runs in O(n) time and O(k) auxiliary space. One pass with O(1) set checks; extra memory is one entry per distinct character.

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

    `ch not in out` on a string: O(n·k) time, O(k) space. Each check scans the result. Seen set + list + join: O(n) time, O(k) space. Or simply ''.join(dict.fromkeys(s)).

    What follow-up questions do interviewers ask about Remove Duplicate Characters from a String?

    Remove only adjacent duplicates ('aabba' → 'aba'). Case-insensitive version.