NVIDIA Interview Question
Software Engineer / DevelopersCountry: United States
My approach would be to create three trees of height 7. Lets say the number is 800-123-4567 and 1=> a,b,c 2=>d,e,f ...
so tree one will be with root a and children def. tree two will be with root b and children d,e,f.. and each d, e, and f will have their own .. then it will be direct tree traversal from root to leaf will represent one phone number..
#include <stdio.h>
#include <string.h>
char *map[10] = {"!", ",", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
void generatePermute(char *num, int pos, int length)
{
static char permute[7];
int i;
if(pos==length)
{
permute[length] = '\0';
printf("%s\n", permute);
return;
}
for(i = 0 ; i < strlen(map[num[pos]-'0']) ; ++i)
{
permute[pos] = map[num[pos]-'0'][i];
generatePermute(num, pos+1, length);
}
}
int main()
{
generatePermute("6236537", 0, 7);
}
The solution to me seems to be making a backtracking program that will go through and assign each number to a letter, generating each by using an iterator. This solution would probably take a while however, since it would need a way to know that it's developed all permutations for a number.
- Student November 15, 2013