Challenge - 5 Problems
HashMap Mastery: First Non Repeating Character
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediateOutput of First Non Repeating Character Index
What is the output of the following Rust code that finds the index of the first non-repeating character in the string?
DSA Rust
use std::collections::HashMap; fn first_non_repeating_char(s: &str) -> Option<usize> { let mut counts = HashMap::new(); for ch in s.chars() { *counts.entry(ch).or_insert(0) += 1; } for (i, ch) in s.chars().enumerate() { if counts[&ch] == 1 { return Some(i); } } None } fn main() { let s = "swiss"; match first_non_repeating_char(s) { Some(i) => println!("{}", i), None => println!("-1"), } }
Attempts:
2 left
š” Hint
Count how many times each character appears, then find the first with count 1.
ā Incorrect
The string "swiss" has characters: s(3 times), w(1 time), i(1 time). The first non-repeating character is 'w' at index 1, so the output is 1.
ā Predict Output
intermediateOutput of First Non Repeating Character Index with All Repeats
What is the output of the following Rust code when the input string has all repeating characters?
DSA Rust
use std::collections::HashMap; fn first_non_repeating_char(s: &str) -> Option<usize> { let mut counts = HashMap::new(); for ch in s.chars() { *counts.entry(ch).or_insert(0) += 1; } for (i, ch) in s.chars().enumerate() { if counts[&ch] == 1 { return Some(i); } } None } fn main() { let s = "aabbcc"; match first_non_repeating_char(s) { Some(i) => println!("{}", i), None => println!("-1"), } }
Attempts:
2 left
š” Hint
If no character appears once, the function returns None which prints -1.
ā Incorrect
All characters in "aabbcc" appear twice, so no non-repeating character exists. The function returns None, and the code prints -1.
š§ Debug
advancedIdentify the Error in HashMap Usage
What error does the following Rust code produce when trying to find the first non-repeating character?
DSA Rust
use std::collections::HashMap; fn first_non_repeating_char(s: &str) -> Option<usize> { let mut counts = HashMap::new(); for ch in s.chars() { counts.insert(ch, counts[&ch] + 1); } for (i, ch) in s.chars().enumerate() { if counts[&ch] == 1 { return Some(i); } } None } fn main() { let s = "abc"; match first_non_repeating_char(s) { Some(i) => println!("{}", i), None => println!("-1"), } }
Attempts:
2 left
š” Hint
Check how counts[&ch] is accessed before insertion.
ā Incorrect
The code tries to access counts[&ch] before the key exists in the HashMap, causing a panic at runtime due to missing entry.
ā Predict Output
advancedOutput of First Non Repeating Character Index with Unicode
What is the output of the following Rust code when the input string contains Unicode characters?
DSA Rust
use std::collections::HashMap; fn first_non_repeating_char(s: &str) -> Option<usize> { let mut counts = HashMap::new(); for ch in s.chars() { *counts.entry(ch).or_insert(0) += 1; } for (i, ch) in s.chars().enumerate() { if counts[&ch] == 1 { return Some(i); } } None } fn main() { let s = "aƔa"; match first_non_repeating_char(s) { Some(i) => println!("{}", i), None => println!("-1"), } }
Attempts:
2 left
š” Hint
Unicode characters are treated as distinct chars.
ā Incorrect
The string "aƔa" has 'a' twice and 'Ɣ' once at index 1. So the first non-repeating character is at index 1.
š§ Conceptual
expertTime Complexity of First Non Repeating Character Using HashMap
What is the time complexity of finding the first non-repeating character in a string of length n using a HashMap to count characters and then scanning again?
Attempts:
2 left
š” Hint
Consider how many times you iterate over the string and the cost of HashMap operations.
ā Incorrect
The algorithm iterates over the string twice: once to count characters and once to find the first unique character.
HashMap operations (insert, lookup) are average O(1), so total time is O(n).
