Microsoft Interview Question for Software Engineer in Tests






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

according to the question 'b' appears first in both s1 and s2
int fn(s1,s2)
{
char a,b;
int i=j=0;
while(s1)
{
a=s1[i++];
while(s2)
{
j=0;
b=s2[j++];
if (a==b)
retun j;
break;
}
}
return -1;
}

- pranav December 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I agree with Pranav, as per the question input (S1 and S2) the answer is 'b'.
The time complexity of Pranav's solution is O(N^2) [ for each char of S1 traverse each char of S2].
Are we allowed to take any other data structure ? the question doesn't talk about it.
Let's say we are allowed to then we can take a HashTable and we will traverse S2 and the persist each char as key, and their position as value. [ in case of duplicate occurance the first one will get place in the HashTabe ].
And then we will traverse S1 and for each char we will do a lookup in the HashTabe ( which is O(1) ). so we will be able to solve this problem in O(N) time compleixty with extra memory[ the HashTable ].

- Gautam December 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

My question is if the ist character of S1 string has two or three occurances in S1. In That scenario, do we have to tell all the occurance or just the ist occurance.
The above code will work for the ist occurance.

- Sam December 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry my mistake " of S1 has two or three occurance in S2 string"

- Anonymous December 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Two strings s1[m], s2[n]. Take temp array A[26]

2. initialize all elements in A[26] to -1 ;

3. for each i=0 to n in S2[n]
if( A[ S2[i] - 'a' ] == -1 )
A[ S2[i] - 'a' ] = i;

4. Now temp array A[26] contains S2[] elements first indexes .

5. Take first element in S1[j] and look in Temp array (A[ S1[j]- 'a' ]), from temp array you will get first index in S[].

6. Time complexity O(n) & Space complexity O(1).

- siva.sai.2020 December 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Siva - You solution looks excellent but I am not clear about your 5th point. What is the meaning of first index? Can you please give me some expamplary idea to make me more clear.

- Sidhu December 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How is the space complexity O(1) if u hv considered a temp array ?

- Sam December 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

space complexity is o(26) , i think this will b the best soln

- snlgaba December 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if name is written in this format Rajeev is S1,and Amar is S2...i mean 1st letter capital.

- anurag September 04, 2011 | Flag


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