Challenge - 5 Problems
Frequency Counter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Frequency Counter for Characters
What is the output of the following Java code that counts character frequencies in a string?
DSA Java
import java.util.HashMap; public class Main { public static void main(String[] args) { String input = "banana"; HashMap<Character, Integer> freq = new HashMap<>(); for (char c : input.toCharArray()) { freq.put(c, freq.getOrDefault(c, 0) + 1); } System.out.println(freq); } }
Attempts:
2 left
💡 Hint
Count how many times each character appears in "banana".
✗ Incorrect
The string "banana" has 1 'b', 3 'a's, and 2 'n's. The HashMap stores these counts as key-value pairs.
❓ Predict Output
intermediateFrequency Counter for Integer Array
What is the output of this Java code that counts frequencies of integers in an array?
DSA Java
import java.util.HashMap; public class Main { public static void main(String[] args) { int[] nums = {2, 3, 2, 4, 3, 2}; HashMap<Integer, Integer> freq = new HashMap<>(); for (int num : nums) { freq.put(num, freq.getOrDefault(num, 0) + 1); } System.out.println(freq); } }
Attempts:
2 left
💡 Hint
Count how many times each number appears in the array.
✗ Incorrect
The number 2 appears 3 times, 3 appears 2 times, and 4 appears once.
🔧 Debug
advancedIdentify the Error in Frequency Counter Code
What error will this Java code produce when trying to count character frequencies?
DSA Java
import java.util.HashMap; public class Main { public static void main(String[] args) { String input = "apple"; HashMap<Character, Integer> freq = new HashMap<>(); for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); freq.put(c, freq.get(c) + 1); } System.out.println(freq); } }
Attempts:
2 left
💡 Hint
Check what happens when freq.get(c) is called for a character not yet in the map.
✗ Incorrect
Calling freq.get(c) for a character not yet in the map returns null. Adding 1 to null causes NullPointerException.
🚀 Application
advancedFind if Two Strings are Anagrams Using Frequency Counter
Which option correctly uses a frequency counter HashMap to check if two strings are anagrams?
Attempts:
2 left
💡 Hint
Anagrams have the same character counts in both strings.
✗ Incorrect
Option A uses frequency counters to count characters in the first string, then reduces counts with the second string. If all counts return to zero, strings are anagrams.
🧠 Conceptual
expertFrequency Counter Pattern Efficiency
What is the time complexity of using a frequency counter HashMap to count characters in a string of length n?
Attempts:
2 left
💡 Hint
Consider how many times each character is visited and the cost of HashMap operations.
✗ Incorrect
Each character is processed once, and HashMap get/put operations are average O(1), so total time is O(n).
