Google Interview Question


Country: United States
Interview Type: Phone Interview




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

There are some other interesting cases:

1. When the last unmatched character marks the beginning of the substring, it will fail. For example, let str = "babad" and substr = "bad".
2. More generally, the code does not handle the cases where the last analyzed characters donĀ“t match substr completely but match a prefix of substr. For example: str = "bababd", substr = "babd"

- Daniel Castro October 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 vote

str = "google"
substr = "ler"

If some prefix of substr (but not all), eg 'le', is the suffix of str this algo will not work. It will return there invalid results instead of null

- Anonymous October 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

The program would fail for a str , substr where str does not contain substr but the last character would match . The program would still return the address of the last character in str which is incorrect.

- Mailman October 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

str = "abc"
substr = "bcde"

- Adarsh October 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

str = "abc"
substr = "bcde"

- Adarsh October 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

str = "google"
substr = "ler"

This algorithm will fail if some prefix (but not the complete string) of the substr is also a suffix of str. eg 'le' a prefix of 'ler' is a suffix of 'google'

- arunprasaath October 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

str = "abcd"
substr = "cdc"

Function falis for above mentioned case. In detail, when first k character(k < substrlen) of substr matches with last k characters of str, this function fails.

Code can be corrected this way

const char* findSubstr(const char* str, const char* substr)
{

    int len = strlen(str);
	int substrlen = strlen(substr);
	int j = 0;
	const char* result = NULL;
	for (int i = 0; i < len; ++i)
	{
		if (str[i] != substr[j])
		{
			j = 0;
			result = NULL;
		}
		else
		{
			if (j==0)
				result = &str[i];

			++j;
			if (j >= substrlen)
				break;
		}
	}
    if(j == substrlen)
	    return result;
    else
        return NULL;
}

- vinayawsm October 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

string = "ddoge" substring = "dog"

- ryangurney October 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I took a look at potentially fixing this to correctly return a substring, and came to the conclusion that if we iterated over every letter in the string, we could keep track of all potential substrings starting from whatever character we're currently looking at. If we find a mismatch, we can throw away that string and recurse using a string containing all characters except for those we have already proven do not contain that substring. Thoughts?

- Telmo October 25, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yep, I was pretty surprised this wasn't mentioned earlier.

- ryangurney October 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

str="ababcd"
substr="abcd"

It fails because it sets j=0 at first mismatch, but doesn't check if the mismatch is the starting of the substring.

- hypothesist October 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the following case will be the problem

original string = "petepeter"
sub string = "peter"

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

original string = cdcdcdy
sub string = cdcdy

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

original string = "cdcdcdcdy"
sub string = "cdcdcdy"

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

const char* findSubstr(const char* str, const char* substr)
{

    int len = strlen(str);
	int substrlen = strlen(substr);
	int j = 0;
	const char* result = NULL;
	for (int i = 0; i < len; )
	{
		if (str[i] != substr[j])
		{
			j = 0;
			result = NULL;
		}
		else
		{
			if (j==0)
				result = &str[i];

			++j;
			++i;
			if (j >= substrlen)
				break;
		}
	}
    if(j == substrlen)
	    return result;
    else
        return NULL;
}

- Anan October 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Also the return string is not null terminated properly

- Tewelde November 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

str = "google"
substr = "ogl"

- lepeisi November 28, 2015 | 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