Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

First Non-Repeating Character Index

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is First Non-Repeating Character Index. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Spotify wants a small utility to quickly analyze short text tokens. Write a Python function that returns the index of the first character in a string that appears exactly once.

Formal Specification

Implement a function first_unique_char(s) where:

  • Input: a string s containing lowercase English letters
  • Output: an integer representing the index of the first non-repeating character
  • Return -1 if every character appears more than once

Notes

A clean solution should avoid comparing every character with every other character. Aim for a linear-time approach using a frequency count, then scan the string again to find the first index whose count is 1.

Constraints

  • 1 <= s.length <= 10^5
  • s contains only lowercase English letters

Function Signature

def first_unique_char(s):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output