# The Lone Character

> It appeared exactly one time. That made it special.

Canonical URL: <https://datadriven.io/problems/the_lone_character>

Domain: Python · Difficulty: easy · Seniority: L5

## Problem

Given a string, return the first character that appears exactly once. Return '' if no such character exists.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **frequency counting and order-preserving lookup**. The challenge is not just finding a unique character but finding the *first* one, which requires a second pass through the string in its original order.

---

### Break down the requirements

#### Step 1: Count character frequencies

Build a dictionary mapping each character to how many times it appears in the string.

#### Step 2: Scan the string again in order

Walk through the original string and return the first character whose count is exactly 1.

#### Step 3: Handle the all-repeats case

If no character has a count of 1, return an empty string.

---

### The solution

**Two-pass frequency scan**

```python
def first_unique_char(s):
    counts = {}
    for ch in s:
        if ch in counts:
            counts[ch] += 1
        else:
            counts[ch] = 1
    for ch in s:
        if counts[ch] == 1:
            return ch
    return '' 
```

> **Time and Space Complexity**
>
> **Time:** O(n) for two passes through the string. Dictionary lookups are O(1) average.
> 
> **Space:** O(k) where k is the alphabet size. For lowercase letters, k is at most 26.

> **Interviewers Watch For**
>
> Why two passes instead of one? A single pass cannot determine which unique character comes first without also tracking insertion order. The two-pass approach is simpler and equally efficient.

> **Common Pitfall**
>
> Returning the character with count 1 from the dictionary instead of from the string. Dictionary iteration order in Python 3.7+ matches insertion order, which happens to work, but iterating the original string is clearer and more intentional.

---

## Common follow-up questions

- Could you solve this with a single pass using an OrderedDict? _(Tests knowledge of ordered data structures and when they add value.)_
- What if the string contained Unicode characters beyond ASCII? _(Tests awareness that the alphabet size k could be much larger than 26.)_
- How would you find the first character that appears exactly k times? _(Tests generalizing the frequency threshold.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_lone_character)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.