PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

Find first unique character evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

  • Medium
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Find first unique character

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Given a lowercase/uppercase alphanumeric string s, return the index of the first character that appears exactly once; if none exists, return -1. Provide an O(n) approach and discuss space–time trade-offs against sorting-based solutions.

Quick Answer: Find first unique character evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Given a string s, return the index of the first character that appears exactly once. If no such character exists, return -1. The comparison is case-sensitive (so 'a' and 'A' are different characters), and the string may contain alphanumeric characters. Provide an O(n) approach. Compared to a sorting-based grouping, the hash-count approach preserves original order without storing extra index metadata and runs in O(n) instead of O(n log n). Example 1: Input: s = "leetcode" Output: 0 ('l' is the first character that appears exactly once.) Example 2: Input: s = "loveleetcode" Output: 2 ('v' at index 2 is the first character to appear exactly once.) Example 3: Input: s = "aabb" Output: -1 (Every character repeats, so there is no unique character.)

Constraints

  • 0 <= len(s) <= 10^5
  • s consists of alphanumeric characters (comparison is case-sensitive).
  • Return -1 when no character appears exactly once (including the empty string).

Examples

Input: ("leetcode",)

Expected Output: 0

Explanation: 'l' is the first character that appears exactly once, at index 0.

Input: ("loveleetcode",)

Expected Output: 2

Explanation: 'l','o','e' all repeat; 'v' at index 2 is the first character appearing exactly once.

Input: ("aabb",)

Expected Output: -1

Explanation: Every character repeats, so no unique character exists.

Input: ("",)

Expected Output: -1

Explanation: Empty string edge case: there is no character to return.

Input: ("z",)

Expected Output: 0

Explanation: Single-character string: that character is trivially unique at index 0.

Input: ("aA",)

Expected Output: 0

Explanation: Case-sensitive: 'a' and 'A' are different characters, so 'a' at index 0 is unique.

Input: ("aabbc",)

Expected Output: 4

Explanation: Last-character-unique case: only 'c' appears once, at index 4.

Input: ("abcabd",)

Expected Output: 2

Explanation: 'a' and 'b' repeat; 'c' at index 2 is the first character that appears exactly once.

Hints

  1. First pass: count how many times each character appears using a hash map.
  2. Second pass: walk the string in order and return the index of the first character whose count is 1.
  3. Scanning the original string (not the map) on the second pass is what preserves the 'first' / original-order requirement.
  4. A sorting-based approach groups equal characters but loses original order unless it also stores indices, and costs O(n log n) versus O(n) here.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Top-p (Nucleus) Sampling in NumPy - Amazon (medium)
  • Implement Multi-Head Attention from Scratch in NumPy - Amazon (medium)
  • Detect and Break a Cycle in a Singly Linked List - Amazon (medium)
  • Caesar Cipher with Translation-Table Optimization - Amazon (medium)
  • Minimum Drone Delivery Time on a Ring of Hubs - Amazon (medium)