Phone Pad Interview Test Solutions in R

If you are interviewing for a programming job you may be asked to demonstrate your skills by solving a programming problem. One classic interview question is the phone pad interview question.

Old phone button pads used for dialing had one number and three or four letters on each key. Each number corresponded to letters so that a user could enter letters using the phone. For example, the button for 2 corresponds to A, B, and C.

What is the Phone Pad Interview Question?

In the phone pad interview test the programmer is asked to write a program that returns all of the possible combinations of letters when the user selects three numbers. For example, if the user presses 2, 3, 4, the program should return:

BDG, BDH, BDI, BEG, BEH, BEI, BFG, BFH, BFI,

CDG, CDH, CDI, CEG, CEH, CEI, CFG, CFH, CFI

Note that two of the keys, 6 and 9, each have four letters associated with them while the other keys each only have three letters.

One Solution Using Loops

One solution I came up with uses loops (scroll down for the complete solution). First create the phone pad in R. Note that I don’t include 0 and 1 because they don’t have any letters associated with them.

```# Create phone pad
two <- c("A", "B", "C")
three <- c("D", "E", "F")
four <- c("G", "H", "I")
five <- c("J", "K", "L")
six <- c("M", "N", "O", "P")
seven <- c("Q", "R", "S")
eight <- c("T", "U", "V")
nine <- c("W", "X", "Y", "Z")```

Next I create some variables to represent the selected numbers.

```# Choose three numbers
number_1 <- two
number_2 <- three
number_3 <- six```

Then I create a variable to save the combinations to. NULL means that the variable is empty.

`combinations <- NULL`

Next create a series of loops. Each loop moves through each of the letters in one of the selected numbers. For example, if the number_1 is 2, the first loop will run three times, once for A, once for B, and once for C. When you put loops within each other like this they are referred to as nested loops.

Within the third loop the paste0 command concatenates the letters from each of the three loops together and enters them in the “combinations” object. For example, if the numbers selected were 2, 3, and 6, the first time R moves through the loops the result will be ADM because the first letter of the first loop is A, the first letter of the second loop is D, and the first letter of the third loop is M.

```# Loop through the letters in number_1
for(i in number_1){

# Loop through the letters in number_2
for(j in number_2){

# Loop through the letters in number_3
for(k in number_3){

# Add each of the letters to the combinations object
combinations <- c(combinations, paste0(i, j, k))

}

}

}```

Finally, print “combinations” so that the user can see the results of the phone pad interview test.

```# Print all of the possible combinations
combinations
"AFP" "BDM" "BDN" "BDO" "BDP" "BEM" "BEN" "BEO" "BEP" "BFM" "BFN"
"BFO" "BFP" "CDM" "CDN" "CDO" "CDP" "CEM" "CEN" "CEO" "CEP" "CFM"
"CFN" "CFO" "CFP"```

Here’s the whole first solution.

```# Create phone pad
two <- c("A", "B", "C")
three <- c("D", "E", "F")
four <- c("G", "H", "I")
five <- c("J", "K", "L")
six <- c("M", "N", "O", "P")
seven <- c("Q", "R", "S")
eight <- c("T", "U", "V")
nine <- c("W", "X", "Y", "Z")

# Choose three numbers
number_1 <- two
number_2 <- three
number_3 <- six

# create an object to save the combinations to
combinations <- NULL

# Loop through the letters in number_1
for(i in number_1){

# Loop through the letters in number_2
for(j in number_2){

# Loop through the letters in number_3
for(k in number_3){

# Add each of the letters to the combinations object
combinations <- c(combinations, paste0(i, j, k))

}

}

}

# Print all of the possible combinations
combinations```

Another Phone Pad Interview Test Answer Using a While Loop and For Loops

The second solution is a Rube Goldberg machine solution that I put together for fun. I wouldn’t recommend using this in a job interview! It works but is complicated and inefficient, using two types of loops and sampling (scroll down for the complete solution).

First create the phone pad in R. Note that I don’t include 0 and 1 because they don’t have any letters associated with them.

```# Create phone pad
two <- c("A", "B", "C")
three <- c("D", "E", "F")
four <- c("G", "H", "I")
five <- c("J", "K", "L")
six <- c("M", "N", "O", "P")
seven <- c("Q", "R", "S")
eight <- c("T", "U", "V")
nine <- c("W", "X", "Y", "Z")```

Next I create some variables to represent the selected numbers.

```# Choose three numbers
number_1 <- two
number_2 <- three
number_3 <- six```

Then create some additional variables to help control and monitor the loops.

```# Create status and count objects
status <- "not enough"
count <- 0```

Now create a while loop. A while loop continues to run until a condition is met that tells it to stop. In this case, the while loop runs as long as the variable status is set to “not enough”.

```# Create a while loop that runs as long as all of
# the possible combinations have not been found
while(status == "not enough"){```

Next create a variable called “combinations”. This happens within the while loop so it will happen every time the while loop runs.

``` # Create combinations object
combinations <- NULL```

Next create a for loop. This runs within the while loop.

A for loop runs a specified number of times. In this case the for loop will run 100 times. Each time the for loop runs R will sample one letter from each of the selected numbers. The sampled letters will be concatenated by the paste0 command and added to the combinations variable.

For example, if the first selected number is 2 then the possible letters that could be sampled from number_1 are A, B, and C. If the second number is three then the possible letters that could be sampled from number_2 are D, E, and F. If the third number is six then the possible letters that could be sampled from number_3 are M, N, O, and P. If A, E, and N are sampled then “AEN” is added to combinations. Each time the while loop runs, the for loop runs 100 times, returning 100 randomly selected three letter combinations.

``` # Sample one letter from each of the chosen numbers
# Add that combination of letters to combinations
# Do this 100 times (needs to run at least as many times as there
# are possible combinations)
for(i in 1:100){
combinations[i] = paste0(sample(number_1, 1), sample(number_2, 1), sample(number_3, 1))
}

rm(i)```

Now check to see if each of the possible combinations have been found. The number of combinations that should be found depends on the number of letters associated with each button pressed. For example, if all three buttons have three letters associated with them then there should be 3 * 3 * 3 = 27 combinations.

If all of the possible combinations have been found the set status to “all possible”.

Note the use of the “unique()” command. This returns only unique values. For example, if you have a vector containing ABZ, ABZ, ABC and you apply unique, it will return ABZ, ABC because the second ABZ is a duplicate.

``` # Check to see if all the possible combinations have been found
# If all of the possible combinations are found, change status from
# "not enough" to "enough" so that the while loop will stop
if((length(number_1) + length(number_2) + length(number_3) == 9 & length(unique(combinations)) == 27) |
(length(number_1) + length(number_2) + length(number_3) == 10 & length(unique(combinations)) == 36) |
(length(number_1) + length(number_2) + length(number_3) == 11 & length(unique(combinations)) == 48) |
(length(number_1) + length(number_2) + length(number_3) == 12 & length(unique(combinations)) == 64)){
status <- "all possible"
}```

Next add one to the count each time the while loop runs.

``` # Add one to count every time the while loop runs
count <- count + 1```

Then print the number of unique letter combinations that were found, print the number of times the while loop has run, and close the while loop.

``` # Print the results of each while loop
print(paste0("length: ", length(unique(combinations))))
print(paste0("count: ", count))
}```

Finally, print the combinations that were found.

```# When the while loop stops, print all of the possible combinations
unique(combinations)```

This solution is fun because it doesn’t solve the problem directly. Instead, R samples the possible letters to make 100 random three letter combinations. If there are enough unique (non-duplicated) three letter combinations that all of the possible combinations must be there, R stops the while loop and prints the combinations.

When this program runs it looks something like the below. In this case, the while loop had to run four times before it returned the right number of unique combinations. Each of the first three times it ran there were 34 unique combinations returned. The fourth time it ran all of the 36 possible combinations were returned. This is interesting because 100 combinations were returned each time. This means that all of the other combinations were duplicates.

If you change the 100 to a higher number R will be more likely to find all of the combinations on each attempt. If you change the 100 to a number that is smaller than the number of possible combinations the while loop will run forever because it won’t be able to find all of the possible combinations.

```"length: 34"
"count: 1"
"length: 34"
"count: 2"
"length: 34"
"count: 3"
"length: 36"
"count: 4"

"AEO" "CEP" "CDM" "CFM" "CDO" "BEM" "AFO" "CDN" "AEP" "CEO" "CFO" "BDP"
"AFP" "ADO" "BFP" "CFP" "BDM" "CDP" "AFM" "AEM" "BEP" "BFM" "BFN" "CEN"```

Here’s the whole second solution.

```# Create phone pad
# O and 1 are not needed because they don't represent any letters
# Note that six and nine have four letters while the others have three
two <- c("A", "B", "C")
three <- c("D", "E", "F")
four <- c("G", "H", "I")
five <- c("J", "K", "L")
six <- c("M", "N", "O", "P")
seven <- c("Q", "R", "S")
eight <- c("T", "U", "V")
nine <- c("W", "X", "Y", "Z")

# Choose three numbers
number_1 <- two
number_2 <- three
number_3 <- six

# Create status and count objects
status <- "not enough"
count <- 0

# Create a while loop that runs as long as all of
# the possible combinations have not been found
while(status == "not enough"){

# Create combinations object
combinations <- NULL

# Sample one letter from each of the chosen numbers
# Add that combination of letters to combinations
# Do this 100 times (needs to run at least as many times as there
# are possible combinations)
for(i in 1:100){
combinations[i] = paste0(sample(number_1, 1), sample(number_2, 1), sample(number_3, 1))
}

rm(i)

# Check to see if all the possible combinations have been found
# If all of the possible combinations are found, change status from
# "not enough" to "enough" so that the while loop will stop
if((length(number_1) + length(number_2) + length(number_3) == 9 & length(unique(combinations)) == 27) |
(length(number_1) + length(number_2) + length(number_3) == 10 & length(unique(combinations)) == 36) |
(length(number_1) + length(number_2) + length(number_3) == 11 & length(unique(combinations)) == 48) |
(length(number_1) + length(number_2) + length(number_3) == 12 & length(unique(combinations)) == 64)){
status <- "all possible"
}

# Add one to count every time the while loop runs
count <- count + 1

# Print the results of each while loop
print(paste0("length: ", length(unique(combinations))))
print(paste0("count: ", count))
}

# When the while loop stops, print all of the possible combinations
unique(combinations)```

You can see a lot of other solutions to the phone pad interview question at Stack Overflow. You can also see the above solutions at GitHub.

Thanks for reading! This website took a great deal of time to create. If it was helpful to you, please show it by sharing with friends, liking, or tweeting! If you have any thoughts regarding this R code please post in the comments.