Google Interview Question for Software Engineer in Tests






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

why not

if src < dst
copy from end to beginning;
else
copy from beginning to end

- chris November 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

void* memcpy(void *src, void *dst,int len){
if (src == NULL || dst == NULL || len <= 0) return NULL ;
if (src - dst < len) { //Overlap
src = src + len ;
dst = dst + len ;
while(len--)
*dst-- = *src-- ;
}
else { // No overlap
while(len--)
*dst++ = *src++ ;
}
return dst ;
}

- Azza October 10, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry the Overlap check should have be(dst-src < len)

- Azza October 10, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How to sovle this problem?

- Carson October 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The simplest solution is ...
void memcpy(void *src, void *dst, int len)
{
while(len--)
*dst++ = *src++;
}

If you want to check errors such as NULL pointer or overlapping of dst and src, then it would be complicated than this.

- Anonymous October 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The overlap thing isn't correct, because adding len to both src and dst, it still preserves the condition (dst - src < len), which will result in overlap again. Get rid of the src = src + len line, and just make the while loop like in the case of no overlap.

- Anonymous November 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes you are right Anonymous.

@Azza the code should be

void* memcpy(void *src, void *dst,int len)
{
if (src == NULL || dst == NULL || len <= 0) return NULL ;


if (dst - src < len)
{ //Overlap
dst = dst + len ;
}

while(len--)
*dst++ = *src++ ;

return dst ;
}

- Cookie May 16, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Azza, looks correct. even if overlap is there, we cant change the address of "dst". above code looks like changing the destination address. isnt it?

- yours.ramesh February 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

One more question: can we copy void* type memory? I guess we have to convert to char* type. Please comment.

- Sadineni.Venkat April 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

agree

- agreer April 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

as far as i know, you have because

ptr+i means ptr+i*sizeof(type)

when ptr is a void pointer, the compiler cannot calculate sizeof(type) since there is no type...

unsigned char would be better

- mal January 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

http://www.vik.cc/daniel/portfolio/memcpy.htm

- trojanhorse June 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I believe memcpy does do the overlap check, memmove does. And memmov basically stores the overlapped ones in a temp location.

The link provided by trojanhorse is interesting. Thanks !

- Smit November 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void my_memcpy(void*  dst, void* src,size_t n ) {

  char* d =  (char *)dst;
  char* s =  (char *)src;

  bool not_overlap = ((d + n) <= src) || ((s + n) <=d);

  if(! not_overlap) {
    std::cerr << "Overlapped dst and src\n";
    return;
  }

  for(int  i = 0 ; i < n ; i ++) {
    *d = *s;
    d++;
    s++;
  }

}

- sparkagain October 03, 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