# Microsoft Internship Written Test

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 "";
}

1
bhanurekha ·

" "

1
AARTHI ·

" "

49
Subhomoy Bakshi ·

why s everyone giving blank inverted commas??? :O

30
Ashish Kothari ·

I guess thats the output of the above code. [3]

49
Subhomoy Bakshi ·

oh! [3]

1
dkg1992 ·

it is longest common subsequence algorithm

1
Debosmit Majumder ·

am i supposed to know this if i`m sitting for jee 12??

1
Md. Shahbaz Siddiqi ·

""

1
rishabh ·

@debosmit obviously not.

1

" "

1

Can anyone explain why it is " "

1
dkg1992 ·

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");
}