Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize an empty frequency counter object.
DSA Typescript
const freqCounter: Record<string, number> = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using [] which is an array, not an object.
Using null or 0 which are not objects.
✗ Incorrect
We use an empty object {} to start counting frequencies by keys.
2fill in blank
mediumComplete the code to increment the count for a character in the frequency counter.
DSA Typescript
freqCounter[char] = (freqCounter[char] || 0) [1] 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using - which would decrease the count.
Using * or / which are not for counting increments.
✗ Incorrect
We add 1 to the existing count or 0 if the key is not present.
3fill in blank
hardFix the error in the code to check if a character exists in the frequency counter.
DSA Typescript
if (freqCounter[1]char) { console.log('Found'); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or curly braces which cause syntax errors.
Using dot notation with variable keys.
✗ Incorrect
Use square brackets [char] to access object properties dynamically.
4fill in blank
hardFill both blanks to create a frequency counter for an array of numbers filtering only even numbers.
DSA Typescript
const freqCounter = {};
for (const num of numbers) {
if (num [1] 2 === 0) {
freqCounter[num] = (freqCounter[num] || 0) [2] 1;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using - or * instead of % for checking evenness.
Using - instead of + for incrementing counts.
✗ Incorrect
Use % to check remainder for even numbers and + to increment count.
5fill in blank
hardFill all three blanks to create a frequency counter for characters in a string, ignoring spaces.
DSA Typescript
const freqCounter: Record<string, number> = {};
for (const [1] of text) {
if ([2] !== ' ') {
freqCounter[[3]] = (freqCounter[[3]] || 0) + 1;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Counting spaces by mistake.
✗ Incorrect
Use 'char' as the loop variable and key to count characters except spaces.
