Qualcomm Interview Question for Software Engineer / Developers






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

Find the code at
http://codepad.org/7DP6z2dF
I wrote this without using any string functions other than strlen.. Using some string functions optimizes it however..

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

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

void removeSubstring(const char* str1, const char* str2, char* out);


void removeSubstring(const char* str1, const char* str2, char* out){
	
	if(!str1 || !str2)
		return;
	int len1 = strlen(str1);
	int len2 = strlen(str2);
	int out_index=0;
	int i = 0; // iterate str1
	int j = 0; // interate str2
	int count = 0;
	int out2= 0;
	
	while (i < len1) {
		j=0;
		if(str1[i] != str2[j])
			out[out_index++] = str1[i++];
		else {
			out2 = out_index;
			for(count=0;j<len2 && i<len1;count++){
				if (str1[i] == str2[j++]) {
					out[out_index++] = str1[i++];
				}
			}
			if(count==len2){
				out_index = out2;
			}
		}
	}
	out[out_index]='\0';
}

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

	char str1[100]={0};
	char str2[10]={0};
	char out[100]={0};	
	
	strcpy(str1, "HelloWorld");
	strcpy(str2, "llo");
	
	removeSubstring(str1, str2, out);
	printf("in:%s\t\t out:%s\n", str1, out);
	
	strcpy(str1, "HelloWorld, hello, hello_");
	removeSubstring(str1, str2, out);
	printf("in:%s\t\t out:%s\n", str1, out);
	
	strcpy(str1, "Hi World!");
	removeSubstring(str1, str2, out);
	printf("in:%s\t\t out:%s\n", str1, out);
	
	return 0;
}

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

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

int main()
{
char str1[]="hello pravin";
char str2[]="hello";

int i=0,j=0,k=0,flag=0, start=0, end=0;
for(i=0;i<strlen(str1);i++)
{

if(str1[i]==str2[j] && flag==0)
{
start=i;
flag=1;
k++;
j++;
}
else if(str1[i]==str2[j] && flag==1)
{
k++;
if(k==strlen(str2)){
end=i;

}
j++;
}
else{printf("\n here");
flag=0;
k=0;
j=0;

}

}
flag=0;
j=0;
printf("\n %d,%d",start,end);
for(i=0;i<strlen(str1);i++)
{
if(i==start)
{
j++;
str1[i]=str1[end+j];
flag=1;
}
else if(flag==1)
{
j++;
str1[i]=str1[end+j];
}
else if(j==(strlen(str1)-end))
{
str1[i]='\0';
}
}
for(i=0;i<strlen(str1);i++)
{
printf("%c",str1[i]);
}

}

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

#include <stdio.h>

void remove_occ(char *, char *);

int main()
{
char str1[20] = "Hello World";
char str2[20] = "llo";

//printf("%s",str1);
remove_occ(str1, str2);
printf("%s",str1);

return 0;
}

void remove_occ(char *str1, char *str2)
{
int count1=0, count2=0;
int temp=0, count1_temp;

while(str1[count1]!='\0')
{
count1_temp=count1;
while(str1[count1] == str2[count2])
{
count1++;
count2++;
}

if(str2[count2] != '\0')
count1=count1_temp;

str1[temp++] = str1[count1++];

count2=0;
}
str1[temp]='\0';
}

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

int removestr(char *s,const char *subs)
{

char *t;
int i=0,j=0;

t = strstr(s,subs);

while(t!=NULL)
{

while(&s[i]!=t)
s[j++] = s[i++];

i += strlen(subs);

t = strstr(&s[i],subs);
}

while(s[i]!='\0')
s[j++]=s[i++];

s[j]='\0';
}

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

I think all the solns are in on2 .Can we do better than this..
I am not able to think about something better than on2.

- kapoor February 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Look at this, this order of n.

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

void StrRemove(char* Src,char *Rstr);

main()
{
char Str[]="I am Vijay Kumar Immadi";
char Str2[]="Vijay";

StrRemove(Str,Str2);
printf("\n%s",Str);

// return 0;

}

void StrRemove(char *Src,char *Rstr)
{

int len1,len2;
int i,j,true,current;


len1=strlen(Src);
len2=strlen(Rstr);
i=0;j=0;true=0;current=0;
while(Src[i]!='\0')
{

Src[current]=Src[i];
if(Src[i]==Rstr[j])
{
j++;

true=1;
}
else if(true)
{
true=0;
j=0;
}
if(j==len2)
{
current=current-j;
j=0;
true=0;
}


i++;
current++;
}

Src[current]='\0';
return;

}

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

nice one...good thinking...

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

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

char *deleteclean(char *,char *);
void main(){
char *s1="aaaaa";
char *s2="cdefag";
char *s3=deleteclean(s1,s2);
printf("%s\n",s3);
}


char *deleteclean(char *s1, char *s2){
char hash[256];
int i;

// hashtable consists of all ASCII characters
for(i=0;i<256;i++)
hash[i]=0;

char *t=s1;

// if a char occurs the hashtbale is updated
while(*t!='\0'){
hash[*t]=1;
t++;
}

int cleanlen=0;
int count=0;
t=s2;
while(*t!='\0'){
if(hash[*t]==1)
count++;
else
cleanlen++;
// All this does is keeps a count of char that occur in s1
t++;
}
cleanlen++; // null terminator
if(cleanlen==0)
return strdup(s2);
// cleanlen is now the length of the string after the char that occur in s1 are removed
// allocates space for a char string of size cleanlen
char *clean=(char *)malloc(cleanlen*sizeof(char));
char *r=clean;
t=s2;
while(*t!='\0'){
if(hash[*t]==0)
*clean++=*t;
t++;
}
*clean='\0';
return r;
}

- Mir May 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ooops! That is not the code. I understood the question wrong. This code removes all the characters that occur in s1 from s2.

Sorry

- Mir May 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="c" line="1" title="CodeMonkey84633" class="run-this">#include<stdio.h>
#include<string.h>
void remove_char(char *string1,char *string2)
{
char *slow,*fast;
slow=fast=str1;
while(*fast!='\0')
{
if(memcmp(fast,str2,strlen(str2))==0)
fast+=strlen(str2);
else
*slow++=*fast++;
}
*slow='\0';
}
int main(int argc,char **argv)
{
char str1[256]="HelloWorld";
char str2[256]="llo";
printf("%s\n",str1);
remove_char(str1,str2);
printf("%s\n",str1);
return 0;
}</pre>

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

char s[20] = "helloworllodpello";
char s1[10] = "llo";
char *pch = strstr(s,s1);

while(pch)
{
strcpy(pch,pch+strlen(s1));
pch = strstr(s,s1);
}

cout << s << endl;

- Anonymous August 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// sry small change.. it should memmove instead of strcpy
char s[20] = "helloworllodpello";
char s1[10] = "llo";
char *pch = strstr(s,s1);

while(pch)
{
memmove(pch,pch+strlen(s1));
pch = strstr(s,s1);
}

cout << s << endl;

- Anonymous August 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String RemoveSubstring(String input, String str) {

		CharSequence seq = str;
		String returnString = input.replace(seq, "");
		return returnString;

	}

- dandy August 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

int main()
{
char *str1 = "Helloworld";
char *str2 = "llo";
char *temp = (char *)malloc(sizeof(char) * 100);

memset(temp, 0, 100);
int i = 0, j = 0, count = 0, str2_len = 0;

for(i = 0; str2[i] != '\0'; i++) {
str2_len++;
}

for(i = 0; str1[i] != '\0'; i++) {
for(j = 0; str2[j]!='\0'; j++) {
if (str2[j] == str1[i+j]) {
count++;
} else {
break;
}
}
if (str2_len == count) {
for(int k = 0; str1[k]!='\0'; k++) {
if( k < i) {
temp[k] = str1[k];
} else if (!((k > i) && (k < j))) {
temp[k] = str1[k+j];
}
}
printf("%s\n",temp);
return 0;
}
count = 0;
}

return -1;
}

- Sukanya January 03, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

int main()
{
	char *str1 = "Helloworld";
	char *str2 = "llo";
	char *temp = (char *)malloc(sizeof(char) * 100);

	memset(temp, 0, 100);
	int i = 0, j = 0, count = 0, str2_len = 0;

	for(i = 0; str2[i] != '\0'; i++) {
		str2_len++;
	}

	for(i = 0; str1[i] != '\0'; i++) {
		for(j = 0; str2[j]!='\0'; j++) {
			if (str2[j] == str1[i+j]) {
				count++;
			} else {
			break;
			}
		}
		if (str2_len == count) {
			for(int k = 0; str1[k]!='\0'; k++) {
				if( k < i) {
					temp[k] = str1[k];
				} else if (!((k > i) && (k < j))) {
					temp[k] = str1[k+j];
				}
			}
			printf("%s\n",temp);
			return 0;
		}
		count = 0;
	}

	return -1;
}

- Sukanya January 03, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

int main()
{
char *str1 = "Helloworld";
char *str2 = "llo";
char *temp = (char *)malloc(sizeof(char) * 100);

memset(temp, 0, 100);
int i = 0, j = 0, count = 0, str2_len = 0;

for(i = 0; str2[i] != '\0'; i++) {
str2_len++;
}

for(i = 0; str1[i] != '\0'; i++) {
for(j = 0; str2[j]!='\0'; j++) {
if (str2[j] == str1[i+j]) {
count++;
} else {
break;
}
}
if (str2_len == count) {
for(int k = 0; str1[k]!='\0'; k++) {
if( k < i) {
temp[k] = str1[k];
} else if (!((k > i) && (k < j))) {
temp[k] = str1[k+j];
}
}
printf("%s\n",temp);
return 0;
}
count = 0;
}

return -1;
}

- sukanya January 03, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

int main()
{
char *str1 = "Helloworld";
char *str2 = "llo";
char *temp = (char *)malloc(sizeof(char) * 100);

memset(temp, 0, 100);
int i = 0, j = 0, count = 0, str2_len = 0;

for(i = 0; str2[i] != '\0'; i++) {
str2_len++;
}

for(i = 0; str1[i] != '\0'; i++) {
for(j = 0; str2[j]!='\0'; j++) {
if (str2[j] == str1[i+j]) {
count++;
} else {
break;
}
}
if (str2_len == count) {
for(int k = 0; str1[k]!='\0'; k++) {
if( k < i) {
temp[k] = str1[k];
} else if (!((k > i) && (k < j))) {
temp[k] = str1[k+j];
}
}
printf("%s\n",temp);
return 0;
}
count = 0;
}

return -1;
}

- sukanya January 03, 2021 | 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