Google Interview Question

write a program to translate alphanumeric phone number to numbers only

Interview Answers

Anonymous

Oct 27, 2011

You're saying you can't come up with a 10 line solution in 30 minutes but want a job at google? Once you came up with the crazy hashmap idea you had already failed the interview .... the solution to every problem is not a hashmap, especially when a simple lookup table will do.

5

Anonymous

Oct 22, 2011

Why wouldn't you map ..... ..? it reduces complexity of retrieval.

2

Anonymous

Apr 20, 2012

Yeah, this is a very simple problem IMO. As she suggested, you can just consider alphanumerics so you don't really have to worry about the input. Also you can convert all characters to small caps or big caps to make things easy. Then use a simple array of size 26 to map a character to its corresponding number, go through the input number and replace each character with the value in the array.

1

Anonymous

Oct 20, 2012

{{{ void convert_numeric(char a[]) { char *p = a; char *post = a; char map[27] = {0}; strcpy(map,"22233344455566677778889999"); while(*p != '\0') { if((*p >= 'a') && (*p = 'A') && (*p = '0') && (*p <= '9')) { *post = *p; post++; } p++; } *post = '\0'; } }}}

Anonymous

Jan 17, 2012

public class PhoneConverter { public static List convertPhone (List list){ int[] values = new int[26]; int i=2; int j=0; for (char tag='a'; tag temp = new LinkedList(); for (Character c : list){ if (Character.isDigit(c)){ temp.add(Integer.parseInt(c.toString())); } else{ temp.add(values[c-'a']); } } return temp; } public static void main(String[] args){ List input = new ArrayList(); input.add('1'); input.add('8'); input.add('0'); input.add('0'); input.add('g'); input.add('o'); input.add('s'); input.add('z'); List output = convertPhone(input); for (Integer num : output){ System.out.println(num); } } }

1

Anonymous

Oct 17, 2011

Actual translation is easy. Store information is a hashmap (key,value) for example (abc, 2), (def,3) ...(wxyz, 9). You can get the information back very easily in O(1) time. Even when you have 20 characters in the alphanumeric string: 1(800)gofedex. It would still be very efficient O(20) etc...But extracting the information you need is somewhat complicated because you can't control what a user enters...lots of cases to consider...not in a 30 minute phone interview.