Beginner Problem Solving Patterns Part 1

Osgood Gunawan
3 min readSep 28, 2020

Many people who are new with problem-solving skills in algorithms have difficulty solving questions in coding challenges. However, specific techniques help your ability to tackle these types of problems more manageable. Usually, all of these problem-solving interview questions are similar, long enough you can see repeated patterns over and over again. So the question that everyone asks is, what is the first step to improve your problem-solving skills? There are two ways to improve your problem-solving skills; one is the planning for solving the problems, which I wrote an article about it before (Best Practice of solving algorithms). The second is master common problem-solving patterns. There are many of these problem-solving patterns, but here we will talk about one of the most common beginner’s problem-solving patterns.

Frequency Counter

Frequency Counter patterns are prevalent in interview questions. It is usually involved with a hash table, which is an object that has keys and values. Most frequency counter questions dealing with strings or numbers.

  • This pattern uses objects or sets to collect values/frequencies of values
  • This can often avoid the need for nested loops or O(N²) operations with arrays/strings

Example:

let's look at one of the popular questions in the frequency counter.

https://binarysearch.com/problems/Anagram-Checks
https://binarysearch.com/problems/Anagram-Checks

Solution

First, check if both strings are the same length (this is a crucial step). Then, we utilize a hash table to review the letter frequency for s0. With the hash table frequency we have for s0, we can do the same with looping through s1 and check if the letter in s1 exists. If it exists, we need to update the letter frequency value by decrementing 1 (hash[str]- -, this is to avoid if the letter appears again). Otherwise, return false.

To understand more in Example 1: we count the word listen for each letter frequency from s0. From there, we can check for s1 string if all the strings are in s0.

So there you go, that is it for frequency counter problem-solving pattern. Practice more with hash table problems. You will more likely encounter a lot of frequency counter problems. Once again, thank you for reading; the next topic for beginner problem-solving patterns will be two pointers, so stay tuned!

--

--

Osgood Gunawan

UX designer | Software Engineer | Dancer | ETL Developer | Data Migration. More about me : https://www.osgood1024.com/