PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to work with hash maps and character frequency counting in string processing. It tests practical coding proficiency, including efficient linear scanning and index tracking, and is commonly used to assess fundamental data structure skills in coding interviews.

  • medium
  • J.P. Morgan
  • Coding & Algorithms
  • Software Engineer

First Non-Repeating Character in a String

Company: J.P. Morgan

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## First Non-Repeating Character in a String Given a string `s`, find the **first character that appears exactly once** when scanning the string from left to right. Return the **0-based index** of that character. If every character repeats (or the string contains no such unique character), return `-1`. ### Examples ``` Input: s = "leetcode" Output: 0 Explanation: 'l' is the first character that appears only once. It is at index 0. Input: s = "loveleetcode" Output: 2 Explanation: 'l', 'o' both repeat; 'v' (index 2) is the first character that appears exactly once. Input: s = "aabb" Output: -1 Explanation: Every character repeats, so there is no non-repeating character. ``` ### Constraints - `1 <= s.length <= 10^5` - `s` consists of lowercase English letters only (`'a'`–`'z'`). - "Non-repeating" means the character occurs exactly once in the entire string. - The answer must reflect the **first** such character by index, not by where it stops repeating.

Quick Answer: This question evaluates a candidate's ability to work with hash maps and character frequency counting in string processing. It tests practical coding proficiency, including efficient linear scanning and index tracking, and is commonly used to assess fundamental data structure skills in coding interviews.

Given a string `s`, find the **first character that appears exactly once** when scanning the string from left to right. Return the **0-based index** of that character. If every character repeats (or the string contains no such unique character), return `-1`. ### Examples ``` Input: s = "leetcode" Output: 0 Explanation: 'l' is the first character that appears only once. It is at index 0. Input: s = "loveleetcode" Output: 2 Explanation: 'l', 'o' both repeat; 'v' (index 2) is the first character that appears exactly once. Input: s = "aabb" Output: -1 Explanation: Every character repeats, so there is no non-repeating character. ``` ### Constraints - `1 <= s.length <= 10^5` - `s` consists of lowercase English letters only (`'a'`-`'z'`). - "Non-repeating" means the character occurs exactly once in the entire string. - The answer must reflect the **first** such character by index, not by where it stops repeating.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of lowercase English letters only ('a'-'z').
  • "Non-repeating" means the character occurs exactly once in the entire string.
  • Return the index of the FIRST such character; return -1 if none exists.

Examples

Input: ("leetcode",)

Expected Output: 0

Explanation: 'l' appears once and is the earliest such character, at index 0.

Input: ("loveleetcode",)

Expected Output: 2

Explanation: 'l' and 'o' both repeat; 'v' at index 2 is the first character appearing exactly once.

Input: ("aabb",)

Expected Output: -1

Explanation: Every character ('a','b') appears twice, so there is no non-repeating character.

Input: ("z",)

Expected Output: 0

Explanation: Single-character string: 'z' appears once, so index 0 is returned.

Input: ("aabbccddx",)

Expected Output: 8

Explanation: All of a,b,c,d repeat; 'x' is the only unique character, located at the last index 8.

Input: ("cc",)

Expected Output: -1

Explanation: 'c' appears twice, so no character is non-repeating.

Hints

  1. You need to know a character's total count across the whole string before you can decide it is unique, so a single left-to-right pass that decides on the spot is not enough — count first, then scan.
  2. Build a frequency map of every character in one pass (a fixed-size array of 26 works since input is lowercase letters).
  3. Do a second pass over the string in order and return the index of the first character whose count is exactly 1. If the loop finishes, return -1.
Last updated: Jul 1, 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
  • 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

  • Two Sum — Indices Summing to a Target - J.P. Morgan (medium)
  • Shift Non-Zero Elements Left In Place - J.P. Morgan (medium)
  • Can All Courses Be Completed? - J.P. Morgan (medium)
  • Merge Overlapping Intervals - J.P. Morgan (medium)
  • Implement Integer Square Root - J.P. Morgan (medium)