Microsoft Interview Question for Software Engineer in Tests






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

The complexity is so ugly O(n^2) which make N^3 for this problem.
public static String remRepeated(String str_, int len)
{
StringBuffer bf = new StringBuffer();

if(len >= str_.length())
return str_;
//Initialize checker
int bgn = 0; int cur;
boolean rept = false;

while(bgn+2*len <= str_.length()){
cur = bgn+len;
while (cur+len < str_.length()) {
rept = true;
//isRepeated?
for(int i=0;i<len;i++,cur++){
if(str_.charAt(i+bgn)!=str_.charAt(cur)){
rept = false;
break;
}
}//end for
if(rept){
//remove repeat
bf.append(str_.substring(0,bgn+len)); //first half
bf.append(str_.substring(cur)); //send half
str_ = bf.toString();
bf.setLength(0);
cur = bgn+len;
}else{break;}
}//end while
bgn++;
}
return str_;
}

- Saimok February 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
#include<string.h>
int len;
void left_shift(char *,int ,int);
void main(){
char str[50];
int i,f=1,rem=1,k=1,j;
clrscr();
printf("ENTER THE STRING");
scanf("%s",str);
len=strlen(str);
while(rem<5){

for(i=0;i<len;i++){
if(str[i]==str[i+k]){
for(j=i;j<i+k;j++)
if(str[j]!=str[j+k]){
f=0;
break; }
if(f==1){
left_shift(str,i+k,k);
i--;
}
}
f=1;
}
k++; rem--;
}
for(i=0;i<len;i++)printf("%c",str[i]);
getch();
}
void left_shift(char *str,int n,int p ){
int i;
for(i=n;str[i]!='\0';i++)
str[i]=str[i+p];
len-=p;
}

- Debanjan February 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

runtime error in this code

- aryan March 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Working Code:

char *p, *q, *save_p;
for(int i = 1; i <= (strlen(text)/2); i++)
{
p = text;
q = text+i;
do{
if(!strncmp(p,q,i)){
save_p = p;
while(*q)
*p++ = *q++;
*p = '\0';
p = save_p; q = save_p+i;
}
p++; q++;
}while(*(q+i));
}
printf("\n TEXT = %s", text);

- Annamalai February 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

your code seems to be wrong, because it outputs "abcecced"

whereas the correct output should be:

abcababceccced
abcababceced
abcabced
abced

- Anonymous March 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String rmRep(String str)
{ int len = str.length();
int p,q;
for(int i=1;i<len/2;i++)
{ p=0;
q=p+i;
String s1,s2;
do{
s1=str.substring(p, q+1);
s2=str.substring(q+1, q+i+1);
if(s1.equals(s2))
{ String str_left= str.substring(0, p);
String str_right= str.substring(q+1);
str= str_left+str_right;
p++;
q=p+i;
}
}while(q<len);
}
return str;
}

- coolforg$ February 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void removeRepeat(char *s, int patternLength)
{
    if(s && patternLength)
    {
        int strlength=strlen(s);

        if(strlength>patternLength)
        {
            int tokenbegin=0;
            int patterni=0;
            int pos=0;
            int newstringi=tokenbegin+patternLength;
            int checki=tokenbegin + patternLength;

            while(s[checki]!='\0')
            {

                if(s[checki+pos]==s[patterni+pos])
                {
                    pos++;

                    if(pos==patternLength)
                    {
                        pos=0;
                        checki+=patternLength;
                    }

                }
                else
                {
                    for(int i=pos;i>=0;--i)
                        s[newstringi++]=s[checki++];
                    patterni++;
                    pos=0;
                }

                if(checki>strlength)
                    break;

            }

            s[newstringi++]='\0';
        }
    }
}

int main(int argc, char *argv[])
{
    
    char *c="abcababceccced";
    char *cp=new char[1024];
    strcpy(cp,c);
    removeRepeat(cp,1);
    printf("%s\n",cp);
    removeRepeat(cp,2);
    printf("%s\n",cp);
}

- Anonymous March 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

my output after 2 iterations:

abcababceced
abcabced

- Anonymous March 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The following code should work, argument is the length of substring

static public String removeRepeat(String input, int number){
		StringBuffer sb=new StringBuffer();
		sb.append(input.substring(0, number));
		for(int i=number;i<input.length();i++){
			if(i==(input.length()-(number-1))){
				sb.append(input.substring(i));
				break;
			}
			if(!input.substring(i-number, i).equals(input.substring(i, i+number))){
				sb.append(input.substring(i, i+1));
			}
			else
				i+=(number-1);
		}
		return sb.toString();
	}

- Anonymous March 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java Version

static String removeSubstring(String s)
{

for(int i=1;i<=s.length()/2;i++){

for(int k=0;k<s.length()/i - 1;k++)
{
int p = k*i;
System.out.println(s.substring(p, p+i) + " " + s.substring(p+i, p+i+i));
if(s.substring(p, p+i).equals(s.substring(p+i, p+i+i)) )
{
s = s.substring(0,p+i) + s.substring(p+i+i,s.length()-1);
System.out.println(s);
k--;
}
}

}
return s;
}

- simone April 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void RemoveDup(string str) {
   int p,q;
   HashMap<string,bool> H;
   for (int i=0; i<str.length; i++) {
      p=0; q=i;
      H.clear(); //empty hash map
      for (; q<str.length; q++) {
         subStr = str.substring(p,(q-p)+1);
         if (H.find(subStr)) str.erase(p, (q-p)+1);  //erase subStr from string
         else H.insert(subStr);
      }
   }
}

Time complexity O(N^3)

- IntwPrep.MS December 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The inner for loop should be

for (; q<str.length; q++,p++) {

- IntwPrep.MS December 30, 2013 | 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