Qualcomm Interview Question Software Engineer / Developers




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

QC means Qualcomm;
And the most correct answer is the code from MS VC++ source code:
char * srstr (const char * str1,
const char * str2)
{
char *cp = (char *) str;
char *s1, *s2;
if (!subStr) return NULL;
while (*cp)
{ s1 = cp;
s2 = (char *) subStr;
while ( *s1 && *s2 && !(*s1-*s2) )
s1++, s2++;
if (!*s2) return(cp);
cp++;
}
return NULL;
}

- BayE on June 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hopefully this works...

#include<stdio.h>

char* xstrstr (char * source, char * possible_substr)
{
	char* p1 = source;
	char* p2 = possible_substr;
	char* retPtr;

	do
	{
		p2 = possible_substr;
		while ( *p1 != *p2 )
		{
			if ( *p1 == '\0'  )
				return NULL;	
			p1++;
		}
		retPtr = p1;
		while( *p1 == *p2 )
		{
			if ( *p1 == '\0'  )
				break;	
			p1++;
			p2++;
		}
		if ( *p2 == '\0' )
			return retPtr ;
	}while(*p1 != '\0' );

	return NULL;

}
int main(int argc,char** argv)
{
	char* ptr = xstrstr(argv[1],argv[2]);
	if ( ptr != NULL)
	{
		printf("%s\n",ptr);
	}
}

- Lord Darth Plagueis on June 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Certainly looks good! Cheers

- amit on September 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It does not work for some cases

- Balu on June 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char* mystrstr( const char* s2, const char * s1) {
char * res = NULL;

if (!*s1)
return s2;

while(*s2) {
//putchar(*s2);
if (*s2 != *s1)
s2++;
else { // they match
res = s2;
printf("\nthey match\n");
while ( *s2 && *s1 ) {
if (*s2 != *s1) {
printf("\n inside if .. s2 is %s and s1 is %s\n", s2, s1);
// putchar(*s2);
// putchar('\n');
// putchar(*s1);
res = NULL;
break;
}
s2++;
s1++;
}
}
}



if ( *s1 != '\0')
res = NULL;

return res;

}

- NewKidOnBlock on June 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Without debugging stuff this time -
{{
char* mystrstr( const char* s2, const char * s1) {
char * res = NULL;

if (!*s1)
return s2;

while(*s2) {
if (*s2 != *s1)
s2++;
else { // they match
res = s2;
while ( *s2 && *s1 ) {
if (*s2 != *s1) {
res = NULL;
break;
}
s2++;
s1++;
}
}
}



if ( *s1 != '\0')
res = NULL;

return res;
}
}}

- NewKidOnBlock on June 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char *strstr(char *str1,const char*str2)
{
//May not be the fastest possible solution, but it should work fine and is ridiculously readable.
for(int i=0;str1[i];i++)
{
for(int j=0;str2[i];j++)
{
if(str1[i+j]!=str2[i+j])
{
if(str1[i+j]==0)
return NULL;
break;
}
if(!str2[j])
return str1[i];
}
}
return NULL;
}

- Tiak on July 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Once more, with whitespace...  If I'm going to promote readability at least.
char *strstr(char *str1,const char*str2)
{
	for(int i=0;str1[i];i++)
	{
		for(int j=0;str2[i];j++)
		{
			if(str1[i+j]!=str2[i+j])
			{
				if(str1[i+j]==0)
					return NULL;
			break;
		}
		if(!str2[j])
			return str1[i];
		}
	}
	return NULL;
}

- Tiak on July 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

const char* myStrStr2(const char str1[], const char str2[])
{
if ((!str1)||(!str2))
return NULL;

const char* p1 = str1;
const char* p2 = str2;
int n = 0;
int n2 = 0;

while (*p2!=0)
{++n2;++p2;}

int* ind2 = new int[n2-1];
const char* ptemp;

for (int i=n2-2;i>=0;--i)
{
ind2[i] = i+1;
}
for (int i=n2-2;i>0;--i)
{
ptemp = str2+i;
p2 = str2;
while((*ptemp!=0)&&(*ptemp==*p2))
{
ind2[ptemp-str2] = ind2[ptemp-str2]<i?ind2[ptemp-str2]:i;
++p2;++ptemp;

}

}
p2 = str2;

while(*p1!=0)
{
if (*p1==*p2)
{
++p1;++p2;++n;
}
else
{
if(n!=0)
{
p2 = p2-ind2[n-1];
n = n-ind2[n-1];
}
else
{
++p1;
++p2;
}
}
if (*p2==0)
{
delete [] ind2;
return (p1-n);
}
}
delete [] ind2;
return NULL;
}

- Anonymous on July 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

char* mystrstr(char* src, char* substr){
char* psrc = src;
char* psubstr = substr;
char* presult = NULL;
if (src == NULL || substr == NULL){
return NULL;
}

do{
presult = psrc;
while(*psrc++ == *psubstr++){
if (*psubstr == '\0')
return presult;
}
psubstr = substr;
}while(*psrc != '\0');
return NULL;
}

int main(int argc, char** argv){
char* ptr = mystrstr(argv[1], argv[2]);
if(ptr != NULL)
printf("%s\n", ptr);
else
printf("no match found\n");
return 0;
}

- sw on July 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char * strstr(char * src, const char * match)
{
   int loop;
   int len_src = 0;
   int len_mat = 0;
   char * p_src;
   char * p_mat;

   p_src = src; 
   while(*p_src != 0x00) len_src++;
   p_mat = match;
   while(*p_mat != 0x00) len_mat++;     
   if(len_src == len_mat && *src != *match || len_src < len_mat) {
      return NULL;
   }
   else {
      for(loop = 0; loop < len_src - len_mat; loop++){
         p_mat = match;
         p_src = src + loop;
         if(*p_mat == *p_src) {
            while(*p_mat++ == *p_src++);
            if(*p_mat == 0x00)  return (src + loop);
            else                continue;
         }
         else {
            return NULL;
         }  
      }    
   }
}

- Anonymous on October 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char * strstr(const char * s1, const char * s2) {
   const char *a = s1, *b = s2;
   for (;;)
      if (!*b) return (char *)s1;
      else if (!*a) return NULL;
      else if (*a++ != *b++)  {a = ++s1; b = s2;}
}

- Anonymous on January 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Make a bet with the recruiter that you can write it with no while, for, or goto's.
If they take the bet, make another bet that you can write it with no local variables
other than the parameters. Produce the code below.
Agree that the solution is inefficient

char *strstr(const char *s1, const char *s2) {
   if (!*s2) return (char *)s1;
   if (!*s1) return NULL;
   if ((*s1 == *s2) && (strstr(s1+1, s2+1) == s1 + 1) return (char *)s1;
   return strstr(s1 + 1, s2);
}

- mooreatcmudotedu on January 22, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through 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