Reviewed by Aditya Kumar · Last reviewed 2026-03-24
To find the first non repeating character, the most efficient approach is a two pass strategy using a hash map (dictionary) to store character frequencies. Mechanics and Why The solution involves two…
Red Flag: O(n²) solution (nested loops). Pro-Move: 'Counter is readable; for production we'd handle Unicode normalization (NFD) since users expect á and a to match'—shows production edge-case thinking.
This easy-level Python/Coding question appears frequently in data engineering interviews at companies like Delivery Hero, Dunnhumby, Fragma Data Systems. While less common, it tests deeper understanding that distinguishes strong candidates. Mastering the underlying concepts (python) will help you answer variations of this question confidently.
Start by clearly defining the core concept being asked about. Interviewers want to see that you understand the fundamentals before diving into implementation details. Structure your answer with a definition, then explain the practical application with a concise example. The expert answer includes a code example that demonstrates the implementation pattern.
To find the first non-repeating character, the most efficient approach is a two-pass strategy using a hash map (dictionary) to store character frequencies.
collections.Counter). For each character encountered, increment its count in the map. This pass takes O(N) time, where N is the length of the string.A single pass is insufficient because you cannot definitively know if a character is non-repeating until you've scanned the entire string. A character encountered early might repeat later.
from collections import Counter
def first_non_repeating(s: str) -> str | None:
if not s:
return None # Handle empty string
# First pass: Count character frequencies
counts = Counter(s)
# Second pass: Find the first character with a count of 1
for char in s:
if counts[char] == 1:
return char
return None # No non-repeating character found
None) and strings where all characters repeat (also returning None).
* Case Sensitivity: Clarify with the interviewer if 'A' and 'a' should be treated as the same or different characters. The provided solution is case-sensitive.
* Character Set: The approach works for both ASCII and Unicode characters, as Python dictionaries handle various character types.
* Pythonic Approach: collections.Counter is highly recommended for its conciseness and efficiency in counting frequencies. For older Python versions (pre-3.7) where standard dictionaries didn't guarantee insertion order, collections.OrderedDict could be used for the frequency map to ensure the "first" non-repeating character is correctly identified, though modern Python dicts preserve insertion order.
Red Flag: O(n²) solution (nested loops). Pro-Move: 'Counter is readable; for production we'd handle Unicode normalization (NFD) since users expect á and a to match'—shows production edge-case thinking.
Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.
According to DataEngPrep.tech, this is one of the most frequently asked Python/Coding interview questions, reported at 3 companies. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.