PracHub
QuestionsLearningGuidesInterview 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.

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 9,000+ real questions from top companies.

Product

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding 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

  • Schedule Priority Jobs with Cooldowns - Amazon (medium)
  • Find Paths Across a Weighted Binary Grid - Amazon (medium)
  • Implement Multi-Player Tic-Tac-Toe - Amazon (medium)
  • Compute Edit Distance - Amazon (medium)
  • Minimize Replacements So Equal Product Values Are Contiguous - Amazon (hard)