Credit Suisse Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

hey can i get the ans

- iDevloper October 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

its recursive

- Anonymous October 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can someone please share the logic to solve this problem

- prashant October 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The first design choice is that you'll need some sort of a map somewhere that informs you which characters are associated with which number. A 2d array of characters is good enough in this case, because: 1) Its hardcoded, so you know exactly how much space you'll need. 2) Although the distribution of characters is uneven, (i.e. 2 -> ABC, but 7-> PQRS), you'll only end up wasting 6 bytes overall, which should be acceptable to any sensible interviewer. If you want even greater space efficiency, try a hash map where each number maps to a linked list of characters. But again, but 6 bytes, it isn't worth the trouble, especially when 2d char arrays afford you the luxury of random access.

The second design choice is in what format you receive the input. I'd say a string of numbers is a pretty good option. You could be clever and just use ints or longs or long longs, but then you're limiting yourself.

Ok, onto the algo. Have to point out that the example provided is wrong. So I'll use my own, say 234. Possible permutations are: ADG, ADH, ADI, AEG, AEH, AEI, AFG....
So the way I'm looking at it, it seems like I can fix a letter in the first position, move to the second position, fix a letter there, move to the third position, etc. Eventually I fix a letter at each position. When I've reached the end of the number, I print the string I've just built.

Now, instead of starting over, its easier to just move back one position, iterate over all the possible characters for that number, and print every string. So once I'm done with iterating over the last position, I move back to the second last. Then I continue iterating over the rest of the chars in the second last position and when I'm done, move back to the third last. Hey, what do you know, sounds like recursion!

- soc October 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
		
	char zero[]="0";
	char one[]="1";
	char two[]="abc";
	char three[]="def";
	char four[]="ghi";
	char five[]="jkl";
	char six[]="mno";
	char seven[]="pqrs";
	char eight[]="tuv";
	char nine[]="wxyz";

	map<int, char*> table2;
    table2.insert ( pair<int,char*>(0, zero));
	table2.insert ( pair<int,char*>(1, one));
	table2.insert ( pair<int,char*>(2, two));
	table2.insert ( pair<int,char*>(3, three));
	table2.insert ( pair<int,char*>(4, four));
	table2.insert ( pair<int,char*>(5, five));
	table2.insert ( pair<int,char*>(6, six));
	table2.insert ( pair<int,char*>(7, seven));
	table2.insert ( pair<int,char*>(8, eight));
	table2.insert ( pair<int,char*>(9, nine));
	
	cout<<"Input Telphone Num: (xxx-xxx-xxxx)"<<endl;
    string telStr;
	cin>>telStr;
	string::iterator it;
	for(it=telStr.begin(); it<telStr.end(); it++ )
	{
		if(*it=='-')
			telStr.erase(it);
	}

	
	printTelString(telStr, 0 , "", table2); 
    return 0;
}

void printTelString(string s, int i, string outStr, map<int, char*>table) 
{
	if (i == s.size())  //go to the last element, there is no permutation
		cout << outStr << endl;
	else 
	{	
		char curChar=s[i];
		int curNum=atoi(&curChar);
		map<int, char*>::iterator test=table.find(curNum);

		char* cur=table.find(curNum)->second ;
		int j=0;
		while(*(cur+j)!='\0')
		{	
			string old=outStr;
			outStr=outStr+cur[j];
			printTelString(s, i + 1, outStr, table);
			outStr=old;
			++j;
		}
		
	}
}

- Anonymous October 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can you run your program?

- yanhai December 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printcombos(int digs, int count, String a, String temp)
{
for (i=0;i<a[digs[count-1].length();i++)
	printcombos(digs,count-1,a, temp+a[digs[count-1][i]);
}

void printstrings (int number)
{
if (number <=0) {cout << "No combinations";return ();}
int digs[10];int count=0;
String a[][]={{},{},{ABC},{DEF},{GHI},{JKL},{MNO},{PQRS},{TUV},{WXYZ}};
while (number > 0)
{
digs[count++]=number%10;number/=10;
}
String temp="";
printcombos(digs,count,a,temp);
}

- vini December 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printcombos(int digs, int count, String a, String temp)
{
if (count==0) {cout << temp;return();}
for (i=0;i<a[digs[count-1].length();i++)
	printcombos(digs,count-1,a, temp+a[digs[count-1][i]);
}

forgot end condition

- vini December 14, 2009 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More