Return the first character whose repeat appears earliest in a Python string using a seen-set in a single O(n) pass. Practise with hidden test cases.
The problem
Scan s left to right and return the first character that you have already seen — i.e. the character whose second occurrence comes earliest.
Return None if no character repeats.
For "abcdefca" the answer is "c": its repeat (index 6) appears before a's (index 7).
Examples
Example 1
Input
first_repeat_char('abcdefca')Expected output
'c'
Example 2
Input
first_repeat_char('abba')Expected output
'b'
+ 3 hidden tests on Submit.
Edge cases to ask about
- No repeats
- Empty string
- Repeat right at the start
Hints
0/3How an interviewer scores this
0/9Your 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.
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.
| Approach | Time | Space | Idea |
|---|---|---|---|
| Check the prefix each time | O(n²) | O(n) | ch in s[:i] slices and scans. |
| bestSeen set | O(n) | O(k) | Return the moment a character is already in the set. |
Walkthrough of the optimal approach (try it yourself first)
Walk the string with a seen set. The first character that is already in seen is the answer — you can return immediately, so the scan often stops early.
There are two reasonable definitions of "first repeating" (earliest second occurrence vs. first character that has any duplicate — that one gives a here). Asking which one the interviewer means is part of the answer.
Complexity: O(n) time, O(k) space. One pass that stops at the first repeat; the set holds at most one entry per distinct character.
Reveal the reference solution
def first_repeat_char(s): seen = set() for ch in s: if ch in seen: return ch seen.add(ch) return None
The brute force, for comparison
def first_repeat_char(s): for i, ch in enumerate(s): if ch in s[:i]: return ch return None
Follow-ups interviewers ask
- Use the other definition: the character with any duplicate that appears first.
- Ignore case and spaces.
Frequently asked interview questions
Core interview concepts, complexities, and follow-ups scored by hiring teams.
What is the time complexity of First Repeating Character in a String in Python?
The optimal solution runs in O(n) time and O(k) auxiliary space. One pass that stops at the first repeat; the set holds at most one entry per distinct character.
What is the brute-force approach, and how do you optimise it?
Check the prefix each time: O(n²) time, O(n) space. `ch in s[:i]` slices and scans. Seen set: O(n) time, O(k) space. Return the moment a character is already in the set.
What follow-up questions do interviewers ask about First Repeating Character in a String?
Use the other definition: the character with any duplicate that appears first. Ignore case and spaces.
