Microsoft Interview Question






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

# include <stdio.h>
#include <iostream>
using namespace std;
int strcspn(char* source, char* search)
{
int index[256];
for(int i=0;i<256;i++)
index[i]=0;
char* ptr=source;
int idx=0;
while(ptr[idx])
{
if(!index[ptr[idx]])
index[ptr[idx]]=idx;
idx++;
}
ptr=search;
idx=0;
while(ptr[idx])
{
if(index[ptr[idx]])
return index[ptr[idx]];
idx++;
}
return -1;
}

- winia August 25, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good

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

Just use strchr

size_t strcspn(const char *s1, const char *s2)
{
size_t ret=0;
while(*s1){
if(strchr(s2,*s1)){
return ret;
else
s1++,ret++;
}
}
return ret;
}

and strchr is essentially

char *strchr(const char *s, int c)
{
while (*s != (char)c)
if (!*s++)return 0;

return (char *)s;
}

- tc September 15, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

my Ans is Place source string in hash and then look for search string in that hash.

- My ans August 25, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good O(n) solution.

- russoue February 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

really nice

- ram August 25, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am not able to follow winia's algo..
Can anybody explain??

- Anonymous August 25, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The key idea of winia's algorithm is to use the index array to store the index of the first appearance of each character in the source array. Then the algorithm loops through the search array and check to see if it has an entry in the index array. If there is, then return the index. Otherwise, keep going.

- QN August 28, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it is a correct algorithm. But the running time is O(n^2) since it searches through an array for each char. We can do O(nlogn) if we use search through a BST

- summer February 27, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if run this program, you'll get the result 2. I think the question has a probelm.

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

int main()
{
char* source = "ttbbcca";
char* search = "ggabba";

printf("result = %d\n", strcspn(source, search));

return 0;
}

- jun August 28, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in the manual, the explanation is like below:
size_t strcspn (const char *string, const char *stopset) [Function]
The strcspn (\string complement span") function returns the length of the initial substring of string that consists entirely of characters that are not members of the set speci ed by the string stopset. (In other words, it returns the off set of the first character in string that is a member of the set stopset.)
For example,
strcspn ("hello, world", " \t\n,.;!?")
-> 5

So, here, we can see that the result should be 2 because the first character which is one of the string 'search' is b.

- jun August 28, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In this case, it is easier. First, parse the search string and put 1 to the 256 table if the character is there. Then parse the source string. If find one match, return current index.

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

anyway, for this question, my answer is like this:

int strcspn(char* source, char* search)
{
int i;
int z_iArrRes[26];

for ( i = 0; i < 26; ++i )
z_iArrRes[i] = -1;

for ( i = 0; i < strlen(source); ++i )
if ( z_iArrRes[*(source + i) - 'a'] == -1 )
z_iArrRes[*(source + i) - 'a'] = i;
for ( i = 0; i < strlen(search); ++i )
if ( z_iArrRes[*(search + i) - 'a'] != -1 )
return z_iArrRes[*(search + i) - 'a'];

return -1;
}

- jun August 28, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int returnindex(char* string1, char* string2)
{
int hash[255]={0};
int l1=strlen(string1),l2=strlen(string2);
for(int ix=0;ix<l1;++ix)
{
if(!hash[string1[ix]])
{
hash[string1[ix]]=1;
for(int iy=0;iy<l2;++iy)
{
if(string1[ix]==string2[iy])
return ix+1;
}
}
}

}

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

btw I think you are looking for strspn.

strcspn computes the length of the maximum initial segment of the string pointed to by s1 which consists entirely of characters NOT from the string pointed to by s2.

In that case it's

size_t strspn(const char *s1, const char *s2)
{
size_t ret=0;
while(*s1 && strchr(s2,*s1++)) ret++;
return ret;
}

- Anonymous September 15, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why cant one just compare individual char locations in the source string and search string, then return the index if the characters at the same index match? Isnt this effecient? Am I missing something?

- Pari September 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your understanding of the question is wrong! Please reread it again

- Devil170 February 12, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int strcspn(char* source, char* search) {
char hash[256];
for (int i=0; i<256; i++) hash[i] = 0;

char* ptr = source;
while(*ptr) {
hash[*ptr]=(!hash[*ptr])?ptr-source:hash[*ptr];
ptr++;
}
ptr = search;
while(*ptr) {
if (hash[*ptr]) return hash[*ptr];
ptr++;
}
return -1;
}

- Sam September 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess the question framed is right! We need to keep the index values of each character in search string and check for the earliest occurence of the character in search string in the source string.

#include <iostream>
using namespace std;

int main(){

	char* source = "abbbbbbkjdfkd";
	char* search = "dkdkkl";

	int arr[256]; // To store the indices of all the search literals
	int ans = -1; // The least index of the search literal
	int index; // The final answer
	
	// Initialize all the array values to -1
	for(int i=0;i < 256; i++){
		arr[i] = -1;
	}

	// Based on the ASCII value put the index of the character in arr.
	for(int i=0; search[i]; i++){
		if(arr[search[i]] == -1) // Takes note of the first occurence of the character and ignores repetition
			arr[search[i]] = i;
			
	}
	
	//Scan the source array and update the index based on the character index
	for(int i=0; source[i]; i++){
		if(ans == -1 && arr[source[i]] != -1){
			index = i;
			ans = arr[source[i]];
		}
		else if(ans > arr[source[i]] && arr[source[i]] != -1){
			index = i;
			ans = arr[source[i]];
		}
	}
	cout << index;
	
	
	return 0;
}

- Devil 170 February 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can build a BST from the source string with a slight modification that each node in the BST also record its index in the source str

We then search each char of the search str in the BST, and return the index if we find a match

total time is O(nlogn)

- summer February 27, 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