Step 2: Find first character with count 1 in original order
Order: s, w, i, s, s. First with count 1 is 'w'.
Final Answer:
'w' -> Option A
Quick Check:
First unique char in 'swiss' = 'w' [OK]
Hint: Check counts, then scan string for first count 1 char [OK]
Common Mistakes:
Returning 's' which repeats
Returning 'i' which is second unique
Returning '_' which means no unique char
4. Identify the error in the following code snippet that tries to find the first non repeating character:
function firstNonRepeatingChar(str) {
const hash = {};
for (let i = 0; i <= str.length; i++) {
const ch = str[i];
hash[ch] = (hash[ch] || 0) + 1;
}
for (let i = 0; i < str.length; i++) {
if (hash[str[i]] === 1) return str[i];
}
return '_';
}
medium
A. The hash is not initialized correctly
B. The second loop should use <= instead of <
C. The first loop uses <= instead of < causing undefined access
D. The return '_' is unreachable code
Solution
Step 1: Check loop conditions for index errors
The first loop uses i <= str.length, which accesses str[str.length] (undefined), causing an error.
Step 2: Correct loop condition to i < str.length
Changing to i < str.length prevents out-of-bounds access and correctly counts characters.
Final Answer:
The first loop uses <= instead of < causing undefined access -> Option C
Quick Check:
Loop index must be less than length [OK]
Hint: Use < not <= for string index loops to avoid undefined [OK]
Common Mistakes:
Using <= causes out-of-range errors
Ignoring loop boundary conditions
Misunderstanding hash initialization
5. Given the string 'aabbccddeeffg', which modification to the code below will correctly return the first non repeating character?
function firstNonRepeatingChar(str) {
const hash = {};
for (const ch of str) {
hash[ch] = (hash[ch] || 0) + 1;
}
for (const ch of str) {
if (hash[ch] === 1) return ch;
}
return '_';
}
Options: A) Change return '_' to return null B) Add a check to ignore spaces in the string C) Modify the function to return '_' if no unique character exists D) Change the hash to store boolean instead of counts
hard
A. Change return '_' to return null
B. Add a check to ignore spaces in the string
C. Modify the function to return '_' if no unique character exists
D. Change the hash to store boolean instead of counts
Solution
Step 1: Analyze the string and current function behavior
String 'aabbccddeeffg' has all characters repeated except 'g' which appears once.
Step 2: Confirm function returns '_' if no unique char
The function already returns '_' if no unique character is found, which is correct behavior.
Step 3: Understand why changing return '_' to return null is better
Changing return '_' to return null clearly indicates no unique character found, which is a better practice than returning a character that might be confused as valid.
Final Answer:
Change return '_' to return null -> Option A
Quick Check:
Return null when no unique char found is clearer [OK]
Hint: Return null to indicate no unique character found [OK]
Common Mistakes:
Returning '_' which can be confused as a valid character
Ignoring spaces when not needed
Using boolean instead of counts loses frequency info
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.