Thread: Interview Question

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    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?

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Last edited by laserlight; 10-01-2010 at 12:34 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by kiros88 View Post
    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;
    }

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by JohnGraham View Post
    Code:
    int foo(void) {
        return 1;
    }
    LOL. Nice one.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Last edited by laserlight; 10-01-2010 at 12:41 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by laserlight View Post
    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".
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by claudiu View Post
    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".
    ... create a function that would take both the strings ...
    that sounds like a request for parameters to me
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by ಠ_ಠ View Post
    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.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by claudiu View Post
    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"
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by ಠ_ಠ View Post
    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.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c Interview question.
    By bkankur in forum C Programming
    Replies: 3
    Last Post: 12-10-2009, 07:53 AM
  2. SDL buffer channels question
    By TriKri in forum Game Programming
    Replies: 3
    Last Post: 12-09-2009, 05:52 PM
  3. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 06:24 AM
  4. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM