Logo

Phone keypad

Return all possible letter combinations a number(s) could represent.

Java

#A one-of-a-kind project

Because it is the first application that is required from me in an interview to be in Java, and I have a 7-day window to finish it (better than the usual ones of 2-hours straight on an online platform), upload it to GitHub, and send the link to them back.

#Idea?

From the old analog phone with clickable buttons for digits, each one of them has a set of characters (about 3 or 4 each) on it, how it works is? It gets an amount of number (4 or less digits to enter), and the output is a list of how many characters can be set from each one. Input is "23," then output is ("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf") with two bonus questions:

  1. Return the answers using an ordering algorithm of your choosing (used DFS).
  2. Cover your code with unit tests.

#Ok, so?

I was excited about this assignment and also in a rush, as I wanted to finish the task on the same day and upload it so I could speed up the process. I spent about 5 hours getting the whole code to run smoothly with the 2 bonus questions in mind as a requirement to do, not an extra. Calling the main function letterCombinations() to do verification first on the numbers.

  • If the input has a special case or letter, then it will be removed.
  • Search the whole input using RegEx.
  • Input can't be less than 0 digits and more than 4 digits.
  • The phone dial-pad doesn't have a character on the 1 button, so the app will remove (or replace with whitespace) it from the input.
  • In case the input has only 1('s), it will throw a custom-made exception outOfScope().

Then after it is over, it passes the number to the brain DFS() function (first requirement), which iterates through the number in a recursive call of itself to get all the possibilities with the other character if more than one number is entered, which means that the returned output will be in ascending order.

#Unit test!!

So so, the whole idea is to make sure that your app passes as much input as possible, whatever is thrown in. Let's say that I hadn't thought of that much before. I sent the assignment first with 5 test cases, all of them with numbers, but it turns out that I also have to include a case when the input is a character—something I didn't expect to come from a digital phone, as it is inspired by it.

I added more verification for special cases and made sure that nothing other than numbers will go through it. Rather than making sure it is only numbers, I was making it check first if it includes them. Maybe there are numbers inside, so the whole input won't be rejected but will take only the numbers from it, remove the character, and check it from start to end—not only the beginning. This is the beauty of RegEx.

With the new modification, unit tests went from 5 unit tests to 16 that include everything as input. Special case at the beginning, middle, and end—checked ✓. Alphabet at the beginning, middle, and end—you got it. Just name the test case and you will find it, also with checking the custom-made exception.

As an experience, it was good as it was the first approach for me to make unit tests in Java (still similar to C#), and using DFS was the same. I also liked that they gave me feedback about the task so I could improve it.