Facebook Interview Question for Software Engineer / Developers






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

The Knuth-Morris-Pratt or the Morris-Pratt algorithms are extensions of the basic Brute Force algorithm. They use precomputed data to skip forward not by 1 character, but by as many as possible for the search to succeed.

Here is some code

void preComputeData(char *x, int m, int Next[])
{
int i, j;
i = 0;
j = Next[0] = -1;

while (i < m)
{
while (j > -1 && x[i] != x[j])
j = Next[j];
Next[++i] = ++j;

}
}


void MorrisPrat(char *x, int m, char *y, int n)
{
int i, j, Next[1000];

/* Preprocessing */
preComputeData(x, m, Next);

/* Searching */
i = j = 0;
while (j < n)
{
while (i > -1 && x[i] != y[j])
i = Next[i];
i++;
j++;
if (i >= m)
{
printf("\nMatch found at : [%d]\n",j - i);
i = Next[i];
}
}
}


int main()
{
char *string="hereroheroero";
char *pattern="hero";

MorrisPrat(pattern,strlen(pattern),string,strlen(string));

printf("\n\n");
return(0);

}

- randiv April 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

rabinkarp string searching algorithm

or use suffix trees....could be build in O(n) and searched using O(m)
n =length of base string
m=length of pattern to be searched

- amm April 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use boyer-moore its more effiecient than all string patter matching

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

I don't think it is a good interview question.

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

int MyStrStr(char * a,char * b)
{
    int a_p = 0;
    int b_p = 0;
    int ret = -1;
    if(a == NULL || b == NULL)
        return -1;
    while(true)
    {
        if(a[a_p] == '\0')
            break;
        if(a[a_p] == b[0])
        {
            ret = a_p;
            b_p = 0;
            //start check
            while(true)
            {
                if(b[b_p] == '\0')
                    break;
                if(a[a_p+b_p] != b[b_p])
                {
                    return -1;
                }
                b_p++;
            }
        }
        a_p++;
    }
    return ret;
}

- Isaac Levy January 30, 2013 | 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;
        if (!*a) return NULL;
        if (*a++ == *b++) continue;
        a = ++s1;
        b = s2;
    }
}

- Anonymous January 20, 2014 | 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