Bird
Raised Fist0
DSA Pythonprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the "First Non Repeating Character Using Hash" algorithm primarily do?
easy
A. Finds the first character in a string that appears only once
B. Counts the total number of characters in a string
C. Sorts characters of a string alphabetically
D. Reverses the string

Solution

  1. Step 1: Understand the problem goal

    The algorithm aims to find the first character in the string that does not repeat anywhere else.
  2. Step 2: Identify the main operation

    It uses a hash (dictionary) to count occurrences and then finds the first character with count 1.
  3. Final Answer:

    Finds the first character in a string that appears only once -> Option A
  4. Quick Check:

    First unique character = first non repeating character [OK]
Hint: Look for the first character with count one in the hash [OK]
Common Mistakes:
  • Thinking it counts total characters instead of unique
  • Confusing sorting with finding unique characters
  • Assuming it reverses the string
2. Which of the following Python code snippets correctly initializes a hash (dictionary) to count characters in a string s?
easy
A. counts = []; for ch in s: counts[ch] += 1
B. counts = {}; for ch in s: counts[ch] = counts.get(ch, 0) + 1
C. counts = set(); for ch in s: counts.add(ch)
D. counts = 0; for ch in s: counts += 1

Solution

  1. Step 1: Check dictionary initialization and update

    counts = {}; for ch in s: counts[ch] = counts.get(ch, 0) + 1 correctly creates an empty dictionary and updates counts using get with default 0.
  2. Step 2: Analyze other options

    counts = []; for ch in s: counts[ch] += 1 uses a list which cannot be indexed by characters; counts = set(); for ch in s: counts.add(ch) uses a set which only stores unique items without counts; counts = 0; for ch in s: counts += 1 uses an integer which cannot store counts per character.
  3. Final Answer:

    counts = {}; for ch in s: counts[ch] = counts.get(ch, 0) + 1 -> Option B
  4. Quick Check:

    Dictionary with get method counts chars correctly [OK]
Hint: Use dict.get(key, 0) to count characters safely [OK]
Common Mistakes:
  • Using list instead of dictionary for counts
  • Using set which does not count duplicates
  • Trying to increment integer without keys
3. What is the output of the following code?
def first_non_repeating(s):
    counts = {}
    for ch in s:
        counts[ch] = counts.get(ch, 0) + 1
    for ch in s:
        if counts[ch] == 1:
            return ch
    return '_'

print(first_non_repeating('swiss'))
medium
A. s
B. _
C. i
D. w

Solution

  1. Step 1: Count character occurrences in 'swiss'

    Counts: s=3, w=1, i=1
  2. Step 2: Find first character with count 1 in order

    Order: s (3), w (1) -> first non repeating is 'w'
  3. Final Answer:

    w -> Option D
  4. Quick Check:

    First unique char in 'swiss' is 'w' [OK]
Hint: Check counts then scan string for first count 1 char [OK]
Common Mistakes:
  • Returning first unique char incorrectly as 's'
  • Confusing 'i' as first unique instead of 'w'
  • Returning '_' when unique char exists
4. The following code is intended to find the first non repeating character but has a bug. What is the bug?
def first_non_repeating_bug(s):
    counts = {}
    for ch in s:
        counts[ch] += 1
    for ch in s:
        if counts[ch] == 1:
            return ch
    return '_'
medium
A. The for loop should iterate over counts.keys() instead of s
B. The return '_' statement is unreachable
C. counts dictionary is not initialized with default values before increment
D. The function does not return anything

Solution

  1. Step 1: Analyze counts dictionary usage

    The code tries to increment counts[ch] without initializing counts[ch], causing KeyError.
  2. Step 2: Identify missing initialization

    Must initialize counts[ch] to 0 before increment or use counts.get(ch, 0) + 1.
  3. Final Answer:

    counts dictionary is not initialized with default values before increment -> Option C
  4. Quick Check:

    Missing initialization causes KeyError [OK]
Hint: Initialize counts[ch] before increment or use get() [OK]
Common Mistakes:
  • Assuming return '_' is unreachable
  • Thinking loop over s is wrong
  • Believing function returns nothing
5. Given the string s = 'aabbccddeeffg', which character will the first non repeating character algorithm return?
Note: The algorithm uses a hash to count characters and returns the first character with count 1 or '_' if none.
medium
A. g
B. a
C. _
D. f

Solution

  1. Step 1: Count occurrences of each character in 'aabbccddeeffg'

    a=2, b=2, c=2, d=2, e=2, f=2, g=1
  2. Step 2: Find first character with count 1 in string order

    All characters except 'g' repeat twice; 'g' appears once at the end.
  3. Final Answer:

    g -> Option A
  4. Quick Check:

    Only 'g' is unique in the string [OK]
Hint: Check counts; last unique char is first non repeating here [OK]
Common Mistakes:
  • Choosing 'a' or 'f' which repeat twice
  • Choosing '_' when unique char exists
  • Ignoring order of characters
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard