Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create an empty hash map for character counts.
DSA Typescript
const charCount: Record<string, number> = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using [] creates an array, not a hash map.
Using null causes runtime errors.
new Map() is a different data structure, not an object.
✗ Incorrect
We use an empty object {} to store character counts as keys and their frequencies as values.
2fill in blank
mediumComplete the code to increment the count of a character in the hash map.
DSA Typescript
charCount[char] = (charCount[char] || 0) [1] 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction decreases the count incorrectly.
Multiplication or division does not increment the count.
✗ Incorrect
We add 1 to the existing count or 0 if the character is not yet in the map.
3fill in blank
hardFix the error in the loop that finds the first non-repeating character.
DSA Typescript
for (const [1] of s) { if (charCount[char] === 1) { return char; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name different from the one inside the loop causes reference errors.
✗ Incorrect
The loop variable should be 'char' to match the usage inside the loop.
4fill in blank
hardFill both blanks to complete the function that returns the first non-repeating character or '_' if none found.
DSA Typescript
function firstNonRepeatingChar(s: string): string {
const charCount: Record<string, number> = {};
for (const [1] of s) {
charCount[[2]] = (charCount[[2]] || 0) + 1;
}
for (const char of s) {
if (charCount[char] === 1) {
return char;
}
}
return '_';
}Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causes undefined key errors.
✗ Incorrect
The loop variable and the key used in the hash map must both be 'char' to count characters correctly.
5fill in blank
hardFill all three blanks to create a dictionary of characters that appear exactly once.
DSA Typescript
const result: Record<string, number> = {};
for (const [3] of s) {
if (charCount[[3]] === 1) {
result[[1]] = [2];
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names causes errors.
Not filtering by count === 1 includes repeating characters.
✗ Incorrect
We use 'char' as the key and 'charCount[char]' as the value for characters that appear once.
