cswork / libraryOpen practice workspace

Coding questions · Amazon

Find first unique character

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

What a Strong Answer Covers

  • A clear algorithm with the right data structures and enough pseudocode or code-level detail to implement it.
  • A correctness argument that explains why the algorithm covers all required cases.
  • Time and space complexity, plus at least one alternative approach when relevant.
  • Focused tests for normal cases, edge cases, and failure modes.

Follow-up Questions

  • How would the approach change if the input were streaming or too large for memory?
  • What invariants would you assert in production code?
  • Which tests would catch off-by-one, duplicate, or tie-breaking bugs?

Find First Unique Character

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).

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.

Solution

Solution Alignment

The prompt asks for an implementation-level answer. The safest way to present it is to define the state, maintain clear invariants, then walk through complexity and tests.

Problem Restatement

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.

Recommended Approach

Scan the string once to count each character, then scan the original string again and return the first index whose count is 1. This preserves original order and is O(n). A sorting-based approach can group equal characters but loses original order unless it stores indices, and it is typically O(n log n).

Correctness

The implementation should maintain an invariant after each loop or operation that directly matches the problem statement. At termination, that invariant implies the returned value has considered every valid candidate exactly once, or has preserved the required data-structure state after every API call.

Complexity

Time: O(n). Space: O(k), where k is the character alphabet size, or O(n) in the worst case for arbitrary Unicode.

Edge Cases and Tests

Empty string, all repeated characters, first character unique, last character unique, case-sensitive input such as "aA", and alphanumeric characters.