Hewlett Packard Interview Question for Tech Leads


Country: India
Interview Type: In-Person




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

Hi, I think following code serves the purpose:

char * strcpy(char * target, const char * source)
{
    char * destination = target;
    while ((*destination++ = *source++) != 0)
	;
    return (destination);
}

Function parameters include source and target, both pointers to char or char array. source remains unchanged while processing so it is declared as const.
It returns char * therefore it can be used directly in printf() like functions.
Yes it has been assumed that destination string is large enough and initialized, else there would be foloowing problems:

-->It might be pointing at memory you don't have access to, in which case it causes a segmentation fault and crashes your program
--> It might be pointing at real data, and if you don't know what it's pointing to, you're causing unpredictable (and very hard to debug) changes to your data.
-->You have no way of knowing if it's been initialized or not - because how do you tell the difference between a valid address and the address that happened to be there when you declared the pointer?

- soni.komal712 August 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

you are returning destination which is pointing '0' in the actual destination-string. You should 'return target'

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

You should return 'return target' not destination which is currently point to char '\0'

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

What is the use of this line ...
char * destination = target;
can't we just ignore "target" in function parameters

- Joseph September 18, 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