Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Character Frequency Counting in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Character Frequency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of character frequency count in a string
What is the output of the following JavaScript code that counts character frequencies in a string?
DSA Javascript
const str = "hello world";
const freq = {};
for (const ch of str) {
  freq[ch] = (freq[ch] || 0) + 1;
}
console.log(freq);
A{"h":1,"e":1,"l":3,"o":1," ":1,"w":1,"r":1,"d":1}
B{"h":1,"e":1,"l":2,"o":2," ":1,"w":1,"r":1,"d":1}
C{"h":1,"e":1,"l":3,"o":2," ":1,"w":1,"r":1,"d":1}
D{"h":1,"e":1,"l":3,"o":2,"w":1,"r":1,"d":1}
Attempts:
2 left
💡 Hint
Count each character carefully, including spaces.
❓ Predict Output
intermediate
2:00remaining
Character frequency count with case sensitivity
What is the output of this code that counts characters in a case-sensitive way?
DSA Javascript
const str = "AaBbAa";
const freq = {};
for (const ch of str) {
  freq[ch] = (freq[ch] || 0) + 1;
}
console.log(freq);
A{"A":2,"a":2,"B":1,"b":1}
B{"A":2,"a":2,"B":2,"b":2}
C{"A":3,"B":2,"a":1,"b":1}
D{"A":2,"a":1,"B":2,"b":1}
Attempts:
2 left
💡 Hint
Uppercase and lowercase letters are counted separately.
🔧 Debug
advanced
2:00remaining
Identify the error in character frequency counting code
What error will this code produce when run?
DSA Javascript
const str = "test";
const freq = new Map();
for (const ch of str) {
  freq[ch] = (freq[ch] || 0) + 1;
}
console.log(freq);
ATypeError: freq[ch] is not a function
BOutput: Map(0) {}
CTypeError: freq[ch] is undefined
DOutput: Map(4) { 't' => 2, 'e' => 1, 's' => 1 }
Attempts:
2 left
💡 Hint
Check how Map objects are accessed and updated.
❓ Predict Output
advanced
2:00remaining
Correct character frequency count using Map
What is the output of this code that correctly counts character frequencies using a Map?
DSA Javascript
const str = "banana";
const freq = new Map();
for (const ch of str) {
  freq.set(ch, (freq.get(ch) || 0) + 1);
}
console.log(Object.fromEntries(freq));
A{"b":1,"a":3,"n":2}
B{"b":1,"a":2,"n":3}
C{"b":1,"a":3,"n":3}
D{"b":2,"a":3,"n":2}
Attempts:
2 left
💡 Hint
Use Map's get and set methods properly.
🧠 Conceptual
expert
2:00remaining
Time complexity of character frequency counting
What is the time complexity of counting character frequencies in a string of length n using a hash map or object in JavaScript?
AO(1) because counting is done instantly
BO(n^2) because each character requires searching the entire string
CO(log n) because hash map operations are logarithmic
DO(n) because each character is processed once and hash map operations are O(1) on average
Attempts:
2 left
💡 Hint
Consider how many times each character is visited and the cost of updating counts.

Practice

(1/5)
1. What does character frequency counting mean in simple terms?
easy
A. Sorting characters alphabetically
B. Counting how many times each character appears in a string
C. Replacing characters with numbers
D. Removing duplicate characters from a string

Solution

  1. Step 1: Understand the meaning of frequency

    Frequency means how often something appears.
  2. Step 2: Apply to characters in a string

    Counting frequency means counting how many times each character shows up in the string.
  3. Final Answer:

    Counting how many times each character appears in a string -> Option B
  4. Quick Check:

    Frequency counting = Counting characters [OK]
Hint: Frequency means count occurrences of each character [OK]
Common Mistakes:
  • Confusing frequency with sorting
  • Thinking frequency means removing duplicates
  • Mixing frequency with character replacement
2. Which of the following JavaScript code snippets correctly initializes an empty object to store character frequencies?
easy
A. const freq = '';
B. const freq = [];
C. const freq = {};
D. const freq = null;

Solution

  1. Step 1: Identify the data structure for frequency counting

    We use an object to store characters as keys and counts as values.
  2. Step 2: Check correct syntax for empty object

    Empty object is created with curly braces {} in JavaScript.
  3. Final Answer:

    const freq = {}; -> Option C
  4. Quick Check:

    Empty object = {} [OK]
Hint: Use curly braces {} to create empty object [OK]
Common Mistakes:
  • Using array [] instead of object {}
  • Using empty string '' instead of object
  • Assigning null which is not an object
3. What will be the output of the following code?
const str = 'hello';
const freq = {};
for (const ch of str) {
  freq[ch] = (freq[ch] || 0) + 1;
}
console.log(freq);
medium
A. {"h":1,"e":1,"l":2,"o":1}
B. {"h":1,"e":1,"l":1,"o":1}
C. {"h":0,"e":0,"l":0,"o":0}
D. SyntaxError

Solution

  1. Step 1: Loop through each character in 'hello'

    Characters are 'h', 'e', 'l', 'l', 'o'.
  2. Step 2: Count frequency using freq[ch] = (freq[ch] || 0) + 1

    For each character, add 1 to its count or start at 1 if not present.
  3. Final Answer:

    {"h":1,"e":1,"l":2,"o":1} -> Option A
  4. Quick Check:

    Count 'l' twice, others once [OK]
Hint: Use freq[ch] = (freq[ch] || 0) + 1 to count [OK]
Common Mistakes:
  • Counting 'l' only once
  • Getting syntax error from code
  • Confusing keys and values in output
4. Find the error in this code that counts character frequency:
const str = 'abc';
const freq = {};
for (let i = 0; i <= str.length; i++) {
  const ch = str[i];
  freq[ch] = (freq[ch] || 0) + 1;
}
console.log(freq);
medium
A. Loop condition should be i < str.length, not i <= str.length
B. freq should be initialized as an array, not object
C. Use let instead of const for freq
D. Missing semicolon after freq[ch] assignment

Solution

  1. Step 1: Check loop range

    Loop runs from i = 0 to i <= str.length, which means i goes up to str.length (3 for 'abc').
  2. Step 2: Identify out-of-bounds access

    str[3] is undefined, so freq[undefined] is updated causing wrong key.
  3. Final Answer:

    Loop condition should be i < str.length, not i <= str.length -> Option A
  4. Quick Check:

    Array index max is length - 1 [OK]
Hint: Use i < length to avoid out-of-bounds errors [OK]
Common Mistakes:
  • Using <= instead of < in loop
  • Initializing freq as array instead of object
  • Ignoring undefined character access
5. Given the string "aabbccddeeffgghhii", which code snippet correctly counts only characters that appear more than once and stores them in freq object?
hard
A. const freq = {}; for (const ch of str) { freq[ch] = (freq[ch] || 0) + 1; } const filtered = {}; for (const ch in freq) { if (freq[ch] > 1) filtered[ch] = freq[ch]; }
B. const freq = {}; for (const ch of str) { if (freq[ch]) freq[ch]++; else freq[ch] = 1; } for (const ch in freq) { if (freq[ch] < 2) freq[ch] = 0; }
C. const freq = {}; for (const ch of str) { freq[ch] = 1; } for (const ch in freq) { if (freq[ch] > 1) delete freq[ch]; }
D. const freq = {}; for (const ch of str) { freq[ch] = (freq[ch] || 0) + 1; } for (const ch in freq) { if (freq[ch] <= 1) delete freq[ch]; }

Solution

  1. Step 1: Count all character frequencies

    Use freq[ch] = (freq[ch] || 0) + 1 to count all characters.
  2. Step 2: Delete characters with frequency <= 1 from freq

    Iterate and delete keys where freq[ch] <= 1 to leave only duplicates in freq.
  3. Final Answer:

    Code snippet that counts all then deletes singles from freq -> Option D
  4. Quick Check:

    Delete freq <= 1 from freq [OK]
Hint: Count all, then delete characters with count <= 1 [OK]
Common Mistakes:
  • Deleting keys while iterating freq object
  • Setting frequency to 0 instead of removing
  • Overwriting counts incorrectly
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