Interview Question for Developer Program Engineers


Country: United States
Interview Type: Phone Interview




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

Hmm.. ok....

Fix this code. That's your homework.

//           { Warning: Brute Forcing below }
// substring function:  will be used by this problem 
// returns index of FIRST starting point of pattern[ ] inside text[ ]
// or returns -1 if pattern[ ] not found in text[ ]
int substring(char text[], char pattern[])
{
    int s;
    int i;    
    for(s=0; s<=strlen(text)-strlen(pattern); s++)            
    {    
        for(i=0; i<strlen(pattern); i++)  //store these strlen's ahead of loops to optimize
        {
            if( pattern[i] != text[s+i] )
                break;
            if (i==strlen(pattern))
                return s;  //found starting at index s of text[ ] string
        }
    }
    return -1; //not found
}


int main(void)

    char *mainstring ="CSK lost to RR and Subhu is a loser";
    char *sub="CSK";
    
    int N = strlen(mainstring);  //length of main string
    int M = strlen(sub);  //length of substring to find
      
    int numsub=0;  //number of substrings
    int t=0;  //index into mainstring[ ]  ... t=0 at start because we want to start searching mainstring from start
    int s;    //position of found substring 
    
    while( (s = substring( &mainstring[t] , sub) ) != -1 )  //search mainstring[] from 't' index (so we don't hit earlier matches)
    {
        printf("A substring found at index %d\n", s);
        numsub++
        t=s+M;  //increase t to s+M so the next "substring" call in while loop will only check past last found substring
    }

    printf("%d substrings founds\n", numsub);
    
    return 0;
}

- S O U N D W A V E October 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

KMP should do here.

- Anoymous October 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String str = "CSK lost to RR";
String subString = "CSK";

int subLength = subString.length();
int count = 0;
int j = 0;
for(int i = 0 ; i < str.length(); ++i) {
if(str.charAt(i) == subString.charAt(j)) {
++j;
if(j == subLength) {
++count;
j=0;
}
}else {
j = 0;
}
}

System.out.println(subString +" is present at "+ count +" places.");

Please make comment if solution is not correct.

- Sharma October 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

CODE FOR SEARCHING ,COUNTING,INDEX OF SUBSTR IN STR:

import java.io.*;
public class Subbu{
public static void main(String args[]){
String str = "CSK lost to RR";
String subString = "CSK";
int i,count;
for(i = 0 ; i < str.length(); ++i) {
if(str.charAt(i) == subString.charAt(i)) {
count=++i;
System.out.println("yes,str contains substr and also substr found at index of "+ count);
}
else{
System.out.println("no, str doesnt contains substr");
}}}}


OUTPUT:
yes,str contains substr and also substr found at index of 1
yes,str contains substr and also substr found at index of 3

- Anonymous October 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

"abab"

search in "abababab"

- anonymous October 09, 2013 | Flag


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