" "
yesterday I got a chance to sit for microsoft internship test...
there were in total 5 questions(75 mins).
Here is one of the question:
You are given with following code snippet, you need to determine what will be the output.And in general tell what the algorithm is doing.
s1="CROCODILE"
s2="MICROSOFT"
c1=9
c2=9
static string PS(string s1, string s2, int c1, int c2)
{
char ch1=s1[c1];
char ch2=s2[c2];
if(c1>=0 && c2>=0)
{
if(ch1==ch2)
{
return PS(s1,s2,c1-1,c2-1)+ch1;
}
else
{
string seq1=PS(s1,s2,c1-1,c2);
string seq2=PS(s1,s2,c1,c2-1);
if(seq1.length()>=seq2.length())
return seq1;
else
return seq2;
}
}
else
return "";
}
-
UP 0 DOWN 0 0 12
12 Answers
hereis equivalent c++ code :
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
string PS(string s1,string s2, int c1, int c2)
{
char ch1=s1[c1];
char ch2=s2[c2];
if(c1>=0 && c2>=0)
{
if(ch1==ch2)
{
return PS(s1,s2,c1-1,c2-1)+ch1;
}
else
{
string seq1=PS(s1,s2,c1-1,c2);
string seq2=PS(s1,s2,c1,c2-1);
if(seq1.size()>=seq2.size())
{
return seq1;
}
else
{
return seq2;
}
}
}
else
return "";
}
int main()
{
string s1="CROCODILE";
string s2="MICROSOFT";
int c1=8;
int c2=8;
cout<<s1.size();
cout<<"solution is :"<<PS(s1,s2,c1,c2)<<"\n";
system("pause");
}