Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing

First Occurrence Map

Python data engineering interview problem. Difficulty: beginner. Pattern: Dictionaries. About 10 minutes. Free to practice.

Map each distinct character to the index of its first occurrence. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Implement first_occurrence_map(s: str) -> dict mapping each distinct character to the index of its first occurrence.

Requirements

  • Keys follow first-seen insertion order.
  • Dict of first indices.

Examples

Input: first_occurrence_map('hello') Output: {'h': 0, 'e': 1, 'l': 2, 'o': 4} Second 'l' is ignored.

Topics: lakebench, python, hash map, strings.

More Python interview questions · All interview problems · Learn data engineering

beginner

First Occurrence Map

Interview-style drill: Map each distinct character to the index of its first occurrence.

Implement `first_occurrence_map(s: str) -> dict` mapping each distinct character to the index of its first occurrence. Example: `"hello"` → `{'h': 0, 'e': 1, 'l': 2, 'o': 4}`. Keep the harness.