Microsoft Interview Question for Software Engineer / Developers






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

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	char str1[] = "HelloWorld123";
	char str2[] = "World";

	int n1 = strlen(str1);
	int n2 = strlen(str2);

	char *s1,s2;
	s1 = str1;
	for(int i=0; s1[i]; i++)
	{
		char *s3=s1+i;
		char *s4 = str2;
		for(;(*s4 == *s3) && (s3!='\0' && s4!= '\0');s3++,s4++ )
		{
		}
		if(*s4 == '\0')
		{
			cout << (i+n2-1) << endl;
			break;
		}
	}

	return 0;
}

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

yup the logic i used was the same.. but since it was the last index.. thought it might be better to start from the right end of the strings and return the first time it matches.

- Anonymous January 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you start to traverse from end then logic needs to be changed to check end of the string s2/s1, by incrementing the counter which is strlen of the strings.

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

Use autometa. N =Input String Length, Time O(N), Space O(1)

/*Assuming that input pointers are valid*/
int patternMatch (char* pInputStr, char* pPattern)
{
int inputLen = strlen (pInputStr);
int patternLen = strlen (pPattern);
int indexInPattern = patternLen - 1;
int indexInInput = inputLen - 1;
int returnIndex = -1;

while (indexInPattern >= 0)
{
if (pInputStr [indexInInput] == pPattern [indexInPattern])
{
indexInPattern --;
}
else
{
indexInPattern = patternLen - 1;
}
if (indexInPattern == -1)
{
returnIndex = indexInInput;
break;
}
indexInInput --;
}
return returnIndex;
}

- Tulley January 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

my first though was using a finite machine, like this one.

but i don't think this is correct.

consider for example:

the string: "12345Worlddkkk"
and the pattern is "World"

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

Use autometa. N =Input String Length, Time O(N), Space O(1)

/*Assuming that input pointers are valid*/
int patternMatch (char* pInputStr, char* pPattern)
{
int inputLen = strlen (pInputStr);
int patternLen = strlen (pPattern);
int indexInPattern = patternLen - 1;
int indexInInput = inputLen - 1;
int returnIndex = -1;

while (indexInPattern >= 0)
{
if (pInputStr [indexInInput] == pPattern [indexInPattern])
{
indexInPattern --;
}
else
{
indexInPattern = patternLen - 1;
}
if (indexInPattern == -1)
{
returnIndex = indexInInput;
break;
}
indexInInput --;
}
return returnIndex;
}

- Tulley January 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int Findsubstring(Char* s1,char* s2)
{
int l=strlen(s1);int m=strlen(s2);
if(m>n)
{
for(int i=0;i<=m-n;i++)
if(strcmp(substr(s1,i,i+n),s2)==0)
return i+n;
}
else
{
for(int i=0;i<=n-m;i++)
if(strcmp(substr(s2,i,i+m),s1)==0)
return i+m;
}
}

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

use KMP or Rabin Carp !

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

int findIndex(char *s1,char *s2){

int len1,len2,i=0,j=0;
len1=strlen(s1);
len2=strlen(s2);

j=0;
for(i=0;i<len1;i++){
if(s1[i]==s2[j])
j++;
else
j=0;

if(j>len2)
return(i);

}
return -1;
}

- Gy February 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int findIndex(char *s1,char *s2){

int len1,len2,i=0,j=0;
len1=strlen(s1);
len2=strlen(s2);

j=0;
for(i=0;i<len1;i++){
if(s1[i]==s2[j])
j++;
else
j=0;

if(j>=len2)
return(i);

}
return -1;
}

- Gy February 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not just use find() and add the length of the string to the position.

- Rishi February 12, 2011 | 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