Index of the First Character That Appears Exactly Once in a String
Company: Autodesk
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Overview: Given a string, return the index of the first character that occurs exactly once in it, or -1 when every character repeats. It tests character frequency counting, case-sensitive comparison and edge cases such as the empty string.
Read the full Autodesk Software Engineer interview experience this question came from
Constraints
- `0 <= len(s) <= 10^5`
- Every character of `s` is printable ASCII (codes 32 to 126), space included.
- The answer is `-1` or an index from `0` to `len(s) - 1`.
Examples
Input: ('',)
Expected Output: -1
Explanation: Empty string: no character occurs once, so the answer is -1.
Input: ('z',)
Expected Output: 0
Explanation: A single character occurs once, at index 0.
Hints
- Whether `s[i]` qualifies depends on every position of the string, both before and after `i`.
- When several characters occur once, the smallest index wins; when none does, including for an empty string, the answer is `-1`.
- 'A' and 'a' are different characters, and a space counts like any other character.