Microsoft Interview Question for Software Engineer / Developers






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

1) find the starting index of substring
a) can be done by KMP in O(n)
b) brute force O(n*m)

2) replacing
a) if the word to be replaced is larger-- then first replace the characters till the length of substring..then use shifting so--> O((l1-l2)*n)
b) if the word to be replaced is small
then just replace snd shift

- gnu October 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

when you are doing the shifting you will end up loosing trailing characters. How you are going to deal with that?

- mr October 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can we use extra array for dealing this question?

- mr October 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think we can just assume the target array have enough memory after '\0'
because every replace string work like that or make error

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

I made some but it is not KMP or something efficient algorithm but it is easy to write front of interviewer.

int main(int argc, char *argv[])
{

int idx = 0;
char target[100];
char *pTag = target;
char sub[10];
char replace[100];
char *pRpr = replace;
if(argc != 4)
{
cout << "Exit... Bye Bye\n";
exit(1);
}

strcpy(target, argv[1]);
strcpy(sub,argv[2]);
strcpy(replace,argv[3]);

//Loop until target 0
char * pTg, * pSb, *pRp;
pRp = replace;
while(pTag != '\0')
{
pTg = pTag;
pSb = sub;
while(*pSb != '\0' && *pTg != '\0' && *pTg++ == *pSb++);
if(*pSb == '\0' && *(pTg -1) == *(pSb -1))
{
while(*pRp != '\0')pRp++;
while((*pRp++ = *pTg++) != '\0');
while((*pTag++ = *pRpr++) != '\0');
cout << target << "\n";
return 0;
}
pTag++;
}
}

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

#include<iostream>
using namespace std;

void replace(char *target, char *substring, char *replace);



int main()
{
char target[100] = "MyNameIsGajanan";
char *substring = "Gajan", *rep = "AAA";
//cout << target <<substring <<replace <<endl;
replace(target,substring,rep);
return 0;
}

void replace(char *target, char *substring, char *rep)
{
char *occur, temp[100]="";

occur = strstr(target,substring);
cout << occur <<endl;

int lensubstring = strlen(substring);
int lenrep = strlen(rep);
strcpy(temp,occur+lensubstring);

strcpy(occur,rep);
strcpy(occur+lenrep,temp);
cout<<"The output is = "<<target <<endl;
return;
}


The output is = MyNameIsAAAan

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

#include <stdio.h>
#include <string.h>

int main ()
{
char *target;
char *pch;
pch = strstr (target,substring);
strncpy (pch,replacement,sizeof(replacement));
puts (target);
return 0;
}

- Hey, will this work? November 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are assuming the replacement is smaller than the original string. For example, it will work if target = "I am very happy", substring = "very" and replacement = "holy" but it wont work if replacement = "No way dude, thats faggy"

- No November 09, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Algo also need to support for replacement of mulitiple occurance of substring with replacement string.

Thx.
Venkatesh.

- Venkatesh November 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Venkatesh: I'm not sure about supporting multiple instances. The question says replace A with B. Not all A with B. Second, not sure if usage of string functions is allowed. If not - need to write your own StrStr and maybe StrLen - which isn't a big deal. Anyway, the question at least occupy 40 mins of interview just to implement + 10 of thinking on algorithm.

- AK June 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can use c++ STL's in-built functions
1: find_first_of(substring). this will give us the index where substring starts
2: copy target string till that point in some other string
3: append the replacement to this string
4: starting index of replacement + sizeof(substring) will give starting index of remainder of starting string
5: append that much to the new string

i am making use of C++ STL inbuilt functions, don't know if its allowed.

- bond December 13, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

OK, enjoy this code. ;) I just implemented helper StrStr function (in the case if we're not allowed to use any string library function)

char* StrStr(char* str, char* strSearch)
{
    char* strResult = NULL;
    if (str && strSearch)
    {
        char *s1 = NULL, *s2 = NULL;
        while (*str && *strSearch)
        {
            s1 = str;
            s2 = strSearch;

            while (*str && *strSearch)
            {
                if (*str != *strSearch)
                {
                    str = s1;
                    strSearch = s2;
                    break;
                }
                str++;
                strSearch++;
            }

            if (!*strSearch)
            {
                strResult = s1;
                break;
            }
            str++;
        }
    }
    return strResult;
}

//
// we assume strTarget have enough space if need to be expanded
//
char* Replace(char* strTarget, char* strSubstr, char* strReplacement)
{
    char *strResult = NULL;
    if (strTarget && strSubstr && strReplacement)
    {
        char *tempSubstr = strSubstr;
        char *tempReplacement = strReplacement;
        char *strEnd = strTarget;
        while (*strEnd)
        {
            strEnd++;
        }
        char *strNewEnd = strEnd;
        
        char *strFound = StrStr(strTarget, strSubstr);
        strResult = strFound;

        while (strFound)
        {
            strTarget = strFound;
            tempSubstr = strSubstr;
            tempReplacement = strReplacement;
            for (;*tempReplacement && *strTarget == *tempSubstr; strTarget++, tempSubstr++)
            {
                *strTarget = *tempReplacement++;
            }

            //
            // handle the situation if substring longer than replacement
            //
            if (*strTarget == *tempSubstr)
            {
                char *strCurrent = strTarget;
                //
                // pass the rest of the substring
                //
                for (;*strTarget == *tempSubstr; strTarget++, tempSubstr++);

                //
                // left shift the rest of the string after replacement
                //
                strEnd = strTarget;
                for (;*strEnd; strCurrent++, strEnd++)
                {
                    *strCurrent = *strEnd;
                }
                *strCurrent = '\0';
                strEnd = strNewEnd = strCurrent;
            }
            else
            {
                //
                // handle the situation if substring shorter than replacement
                //
                if (*tempReplacement)
                {
                    //
                    // right shift the string
                    //
                    unsigned int uDiff = strEnd-strTarget;
                    *(strEnd+1) = '\0';
                    strNewEnd = strEnd+1;
                    for (;uDiff > 0; uDiff--, strEnd--)
                    {
                        *(strEnd+1) = *strEnd;
                    }
                    *(strEnd+1) = *strEnd;
                    strEnd = strNewEnd;

                    //
                    // copy the rest of replacement
                    //
                    for (;*tempReplacement; strTarget++, tempReplacement++)
                    {
                        *strTarget = *tempReplacement;
                    }
                }
            }

            strFound = StrStr(strTarget, strSubstr);
        }
    }

    return strResult;

}

- AK June 01, 2010 | 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