Approaching a Frequency Counter Coding Challenge.

Jas Spencer
3 min readJun 18, 2021

I recently took part in a technical coding challenge that, between you and me, I was dreading. I was so nervous because no matter how hard I practiced I was worried that the problem would be something I had never seen before. Thankfully, I practiced learning how to recognize certain characteristics of challenges and go on from there. This particular challenge was that of a Frequency Counter challenge.

If you would like to no more about practicing for coding challenges, I would highly recommend these Udemy courses by Colt Steele or Stephen Grider, as they do a great job teaching how to approach and solve coding challenges.

A frequency counter challenge is when you need to collect the values or frequencies depending on your input. A great simple challenge (close, but not the exact one I had) to learn the basic logic would be counting the most repeated character in a string.

"aaabccd" returns: 'a':3"hello world" returns: 'l':3"111addrt1z" returns: '1':4

With the examples above, you can not only return the most repeated character, but also the number of times it occurs very easily. The best data structure to use in this situation is an object, hash if you’re using Ruby, dictionary with Python, etc. With an object, you can store the individual characters as keys, and the number of times they occur as they values.

Let’s break this down into smaller steps and walk through each one:

  1. Create an empty object/hash/dictionary. This will also be what is returned at the end.
  2. Go over each character in the string. This should tell us that we need to use a loop.
  3. Store each character in the object.
  4. If the character already exists in the object, increase the value. If not, set its value to 1. (The ‘if’s in this step should be making you think of more logic.
  5. Return the character with the highest value, or the entire object with all counts depending on the challenge requirements.

Now that we have our steps, let’s start with filling in the logic:

function maxChar(str) { //we will be using a string input
const charMap = {} //our empty object
for (let char of str) { //sets the loop to go over each character
if (charMap[char]) {//check to see if a character is in the obj
charMap[char]++ //increase the counter if true
} else {
charMap[char] = 1 //set the value to 1 if it's a new character
}
}
return charMap;
}

This above example will return the entire object with the count of each character in the string, if the challenge asks us to return the most used character, we simply have to add in extra functionality. After filling out our object, we need to figure out which character has the highest count. We start with an additional variable which will eventually contain the character,

function maxChar(str) { //we will be using a string input
const charMap = {} //our empty object
let maxChar = '' //start the variable as nothing
let max = 0 //we need something to compare the characters to
for (let char of str) { //sets the loop to go over each character
if (charMap[char]) {//check to see if a character is in the obj
charMap[char]++ //increase the counter if true
} else {
charMap[char] = 1 //set the value to 1 if it's a new character
}
}

for (let char in charMap) { //loops over each key in the object
if (charMap[char] > max) { //if the character occurs more than 0
max = charMap[char]; // 'max' is assigned that character
maxChar = char; //our maxChar variable is assigned to max
}
}
return maxChar;
}

The logic in both examples is a great way to approach frequency counter challenges. Some modification might be needed depending on how the challenge goes, but more or less this are some good steps to take. Obviously there are more ways than this to solve the problem, but this is how I approached it. As long as you know what to look for, challenges like this become a lot simpler.

--

--

Jas Spencer

Software Engineer in NYC with expertise in Ruby on Rails, JavaScript, and React