Easy
Hash TableStringQueueCounting
Updated Sep 2026

First Unique Character in a String

Asked at Goldman Sachs

Problem

First Unique Character in a String asks for the index of the first character that appears exactly once, or -1 if every character repeats. It is a two-pass counting problem — the classic "count first, then scan in order" pattern.

Asked At

CompanyDifficulty
Goldman SachsEasyView all Goldman Sachs questions →

How to Think About It

1.

Brute force: for each index, scan the whole string to see if that character appears elsewhere. That is O(n²).

2.

Key insight: uniqueness depends on the whole string, but "first" depends on order. So do two passes — one to count, one to find the first character whose count is 1.

3.

Use a 26-length array for lowercase letters. The second pass walks the string (not the alphabet), which preserves the original order.

4.

Walkthrough for "loveleetcode": counts l=2, o=2, v=1, e=4, .... Scanning: l (2), o (2), v (1) -> answer index 2.

5.

Streaming follow-up: if characters arrive one by one, keep counts plus a queue of candidates and pop from the front while the front is no longer unique.

Optimal Approach

Step 1: count = [0] * 26; count every character.
Step 2: For i in 0..n-1: if count[s[i]] == 1, return i.
Step 3: Return -1.

Time: O(n). Space: O(1) (26 counters).

What Trips People Up in Real Interviews

1.

Scanning the alphabet in the second pass instead of the string. That finds the alphabetically first unique letter, not the first by position.

2.

Returning the character instead of its index.

3.

Using s.count(ch) inside a loop — that hides an O(n²) algorithm behind a one-liner.

4.

Forgetting to return -1 when every character repeats.

Solution Code

def firstUniqChar(s):
    count = [0] * 26
    for ch in s:
        count[ord(ch) - ord('a')] += 1
    for i, ch in enumerate(s):
        if count[ord(ch) - ord('a')] == 1:
            return i
    return -1

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the First Unique Character in a String problem?

First Unique Character in a String asks for the index of the first character that appears exactly once, or `-1` if every character repeats. It is a two-pass counting problem — the classic "count first, then scan in order" pattern.

How do you solve First Unique Character in a String?

The optimal approach is described in detail above, including step-by-step walkthroughs, complexity analysis, and solution code in Python. Scroll up to the "Optimal Approach" section.

What companies ask First Unique Character in a String?

First Unique Character in a String is asked at Goldman Sachs. It is a easy difficulty problem.

What are common mistakes on First Unique Character in a String?
  • Scanning the alphabet in the second pass instead of the string. That finds the alphabetically first unique letter, not the first by position.
  • Returning the character instead of its index.
  • Using `s.count(ch)` inside a loop — that hides an `O(n²)` algorithm behind a one-liner.
  • Forgetting to return `-1` when every character repeats.