0
0
DSA Pythonprogramming~20 mins

First Non Repeating Character Using Hash in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Hash Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Find the first non-repeating character index
What is the output of this code snippet that finds the index of the first non-repeating character in the string?
DSA Python
def first_non_repeating_char(s):
    count = {}
    for ch in s:
        count[ch] = count.get(ch, 0) + 1
    for i, ch in enumerate(s):
        if count[ch] == 1:
            return i
    return -1

print(first_non_repeating_char("swiss"))
A1
B2
C3
D-1
Attempts:
2 left
💡 Hint
Check the count of each character and find the first with count 1.
Predict Output
intermediate
2:00remaining
Output of first non-repeating character index for 'leetcode'
What is the output of this code that finds the first non-repeating character index in the string 'leetcode'?
DSA Python
def first_non_repeating_char(s):
    count = {}
    for ch in s:
        count[ch] = count.get(ch, 0) + 1
    for i, ch in enumerate(s):
        if count[ch] == 1:
            return i
    return -1

print(first_non_repeating_char("leetcode"))
A2
B-1
C0
D1
Attempts:
2 left
💡 Hint
Look for the first character that appears only once.
🔧 Debug
advanced
2:00remaining
Identify the error in this code for first non-repeating character
What error does this code raise when run?
DSA Python
def first_non_repeating_char(s):
    count = {}
    for ch in s:
        count[ch] += 1
    for i, ch in enumerate(s):
        if count[ch] == 1:
            return i
    return -1

print(first_non_repeating_char("apple"))
AKeyError
BTypeError
CIndexError
DNo error, output 0
Attempts:
2 left
💡 Hint
Check how the dictionary is updated before keys exist.
🧠 Conceptual
advanced
2:00remaining
Why use a hash map for first non-repeating character?
Why is a hash map (dictionary) the best data structure to find the first non-repeating character in a string?
AIt uses less memory than arrays or lists for counting characters.
BIt sorts the characters automatically to find the first unique one.
CIt stores characters in order and removes duplicates instantly.
DIt allows counting character occurrences in O(n) time and O(1) average lookup.
Attempts:
2 left
💡 Hint
Think about counting and quick lookup.
Predict Output
expert
2:00remaining
Output of code with Unicode characters
What is the output of this code that finds the first non-repeating character index in the Unicode string 'aáa'?
DSA Python
def first_non_repeating_char(s):
    count = {}
    for ch in s:
        count[ch] = count.get(ch, 0) + 1
    for i, ch in enumerate(s):
        if count[ch] == 1:
            return i
    return -1

print(first_non_repeating_char('aáa'))
A0
B1
C-1
D2
Attempts:
2 left
💡 Hint
Unicode characters are distinct; count each separately.