# Interview Question

• 10-01-2010
kiros88
Interview Question
Hi I was wondering if someone could help me with this I always get this question alot in interviews and was hoping someone could help with an answers.

if i had 2 strings str1"abcde" and str2 "bcd" create a function that would take both the strings and see if the str1 contains str2 if so return the index of str1 where the str2 is located. if not return -1

Most of the times im using just a bunch of for loops and if statesment but is there a simpler approach to getting this done?
• 10-01-2010
claudiu
Well to be honest I find interview questions that make you reinvent the wheel completely pointless in assessing someone. There is a function which does this already and it is part of the library.

strstr()

strstr - C++ Reference

I'm pretty sure it's optimally implemented so you might want to take a look at its implementation.
• 10-01-2010
laserlight
If you are being asked to describe an algorithm without actually implementing it, then perhaps the interviewer is looking for something sophisticated, e.g., Boyer-Moore. If you are being asked to implement an algorithm on paper, or on a computer but within a very short period of time, then perhaps the interviewer is just checking that you have practical programming skills to develop and implement a simple algorithm, in which case "a bunch of for loops and if statesment" would suffice.

EDIT:
Or, if there is no requirement to develop a simple algorithm, then an extension of claudiu's suggestion is best, i.e., to demonstrate practical programming skills by writing a wrapper around an existing function to do exactly what is required.
• 10-01-2010
JohnGraham
Quote:

Originally Posted by kiros88
if i had 2 strings str1"abcde" and str2 "bcd" create a function that would take both the strings and see if the str1 contains str2 if so return the index of str1 where the str2 is located. if not return -1

Code:

```int foo(void) {     return 1; }```
• 10-01-2010
claudiu
Quote:

Originally Posted by JohnGraham
Code:

```int foo(void) {     return 1; }```

LOL. Nice one.
• 10-01-2010
laserlight
Quote:

Originally Posted by claudiu
LOL. Nice one.

Except that it demonstrates JohnGraham's failure to read the requirements properly since the function has no parameters. Furthermore, although the language is a little ambiguous, the description does state certain steps that must be done, even if precomputation due to known input was feasible.
• 10-01-2010
claudiu
Quote:

Originally Posted by laserlight
Except that it demonstrates JohnGraham's failure to read the requirements properly since the function has no parameters. Furthermore, although the language is a little ambiguous, the description does state certain steps that must be done, even if precomputation due to known input was feasible.

Actually, if we are talking requirements now, I think John got them spot on. The OP meant to say something else but said exactly what John pointed out. There is nowhere in the OP's original post a mention of the word "parameters".
• 10-01-2010
ಠ_ಠ
Quote:

Originally Posted by claudiu
Actually, if we are talking requirements now, I think John got them spot on. The OP meant to say something else but said exactly what John pointed out. There is nowhere in the OP's original post a mention of the word "parameters".

Quote:

... create a function that would take both the strings ...
that sounds like a request for parameters to me
• 10-01-2010
claudiu
Quote:

Originally Posted by ಠ_ಠ
that sounds like a request for parameters to me

Well, I think we are going off subject here, but it really isn't a request for parameters, or rather not one expressed correctly. "taking the strings" could very well mean storing them in some function variables:

int func(void){
char *str1 = "hello";
char *str2 = "abcd";
}

Vague requirements are not really requirements.
• 10-01-2010
ಠ_ಠ
Quote:

Originally Posted by claudiu
Vague requirements are not really requirements.

if you ever try to implement that belief during an interview, please let me know ahead of time so I can come and watch

and yes, I agree, your example does count as "taking the strings"
• 10-01-2010
claudiu
Quote:

Originally Posted by ಠ_ಠ
if you ever try to implement that belief during an interview, please let me know ahead of time so I can come and watch

and yes, I agree, your example does count as "taking the strings"

I do. I worked as a requirements engineer for several years.
• 10-02-2010
laserlight
Quote:

Originally Posted by claudiu
"taking the strings" could very well mean storing them in some function variables:

Excellent. Explain how did JohnGraham satisfy this interpretation of the "requirements".

Quote:

Originally Posted by claudiu
Vague requirements are not really requirements.

Agreed, but if so, clarify, rather than interpret, or if you really want to interpret, then be bulletproof.