Microsoft Interview Question for Software Engineer in Tests






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

I feel KMP algorithm solves this problem, please refer to below link:
http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm

- Uday October 06, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 0 vote

#include<stdio.h>

int main()
{
char str[] = "xyztheabc";
char st[] = "the";
int i = 0;
int j =0;
int len = strlen(str);

int x=0, y=0;

printf ("\nlen - %d\n",strlen(st));

for(i=0;i<len;i++)
{
if(str[i] == st[j])
{
j++;
printf ("%d\t%d\n",i,j);
}
else if (j>0)
{
j=0;
i--;
}

if(j == strlen(st))
break;
}

if(j == (strlen(st)))
{
printf("\nYES\n");
x = i-2;
y = x + j;
for(;y<len;y++,x++)
str[x] = str[y];
str[x] = '\0';
printf ("\n\n Altered - %s\n",str);
else
printf("\nNO\n");


}


** This code does both sub string finding then removal as well **

- Sarz November 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Test Cases
1. Any input2 with length(input2) > length(input1) should fail
2. input2 = input1 should result in 0 string
3. Give input1 and input2 as null, it should return nothins
4. If input2 is null and input1 is not null, output is same as input1
4. If input2 is not null and inout is null, output should be null

- RemoveMePlease October 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the is a tough question !
first of all one needs to find out where input2 occurs - this could happen multiple times. Then replacement shud happen. What is the known best runtime for solving this ?

- LR October 04, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is tough indeed. All I can think of is O(n^m), I am sure there is a better solution. what is it? n= length of input1 and m = length of input2.

- socialMetric October 04, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package careercup;

public class removeString {

private int index= 0;

public void removeStrings(String src, String remove){
int len = remove.length();

String finalString = new String();

String getString = src.substring(index,index+len);

while(index+len < src.length()){
getString = src.substring(index,index+len);
if(!(remove.equals(getString))){
finalString += getString;
}
index++;
}

System.out.println(finalString);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

removeString obj = new removeString();

obj.removeStrings("abctheabcthabcthetheabc", "the");

}

}

- Laks October 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Oops! corrected code below

package careercup;

public class removeString {

private int index= 0;

public void removeStrings(String src, String remove){
int len = remove.length();
int i = 0;
String finalString = new String();

String getString = src.substring(index,index+len);
while(index+len < src.length()){
//int i = 0;
getString = src.substring(index,index+len);
if(!(remove.equals(getString))){
if(i%3==0)
finalString += getString;
else
;
}
index++;i++;
}

System.out.println(finalString);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

removeString obj = new removeString();

obj.removeStrings("abctheabcthabcthetheabc", "the");

}

}

- Laks October 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 0 votes

your code needs slight improvement.
see the block: if (i%3)..... remove this....

moreover.... right now complexity goes to n^m.
at each index++, you are taking the entire substr and comparing. Now

idea of KMP search shoul be used to reduce complexity.

any comments ?

- Anonymous October 10, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i%3 is necessary because if i am incrementing one character at a time in the input string and so, in the out put string, i dont want all these combinations to be appended.
Ex if my input is abcdef and the output is of length say 3, then i am checking for
abc,bcd,cde,def but in the output string, i only want abcdef and not abc+bcd+cde+def.

- Java Coder October 11, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pattern (Java)

- Simple October 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Get the integer equivalent of the input2 using atoi function.
Count the number of characters in the input2.
Start parsing input1. Stride input1 by a value equal to strlen(input2), lets call this a chunk. If integer value of the chunk is equal to integer value of input2, then don't copy the chunk into output string. Else copy chunk into output string. I suppose this would improve the running time. Please comment.

- Alternate Algo October 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use the API's provided by Java. This will be the most efficient method.

package google;

public class RemoveInput2FromInput1 {

private String a,b;

public RemoveInput2FromInput1(String a, String b) {
// TODO Auto-generated constructor stub

this.a = a;
this.b = b;

assert(!this.a.equals(null));
assert(!this.b.equals(null));
}

private void removeExp(){
String out = a.replaceAll(b,"");
assert(!out.equals(null));
System.out.println(out);
assert(out.equals("abcthabdshhtexyzaaa"));
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
RemoveInput2FromInput1 obj = new RemoveInput2FromInput1("abcthabdtheshhtexyztheaaa","the");
obj.removeExp();
}

}

- Java Coder October 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think it can be done in linear time. Just consider it a pattern recognition and you can use automaton to do all the tricks.

- Xiang October 14, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Almost the same as this one:
http://www.careercup.com/question?id=62468

- XYZ November 21, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I can only figure out a O(N*M) solution:

#include<string>
#include<iostream>
using namespace std;

void main(){
const string input1="abcthabdtheshhtexyztheaaa";
const string input2="the";
if(!input2.size() || input1.size()<input2.size()){
cout<<input1;
return;
}
char c=input2.at(0);
for(string::size_type i=0; i<input1.size(); ){
if(input1.at(i) != c || i>=input1.size()-input2.size()+1){
cout<<input1.at(i);
i++;
}
else{
if(!input2.compare(input1.substr(i,input2.size())))
i=i+input2.size();
else
i++;
}
}
}

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

private string RemoveSubString(String src, String remove)
{
int index = 0;
StringBuilder finalString = new StringBuilder();
int len = remove.Length;

while (index < src.Length)
{
if (src[index] == remove[0])
{
String subString = src.Substring(index, len);
if (subString.Equals(remove))
{
index += len;
}
else
{
finalString.Append(src[index]);
index++;
}
}
else
{
finalString.Append(src[index]);
index++;
}
}
return finalString.ToString();
}

- Anirudh March 22, 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