Google Interview Question


Country: United States
Interview Type: Phone Interview




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

void * memove(void *dest, void *src, size_t n)
{

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

if(s == d)
return dest;

if(s < d) {
//copy from back
s=s+n-1;
d=d+n-1;
while(n--)
{
*d-- = *s--;
}
}
else {
//copy from front
while(n--)
*d++=*s++;
}
return dest;
}

optimisation suggested to use int and float may not work all time as the address need to be exact divisible of 4 or 8 depending on machine and memory alignment.

The while loop can be optimised by using loop unrolling.

This was the answer expected by the interviewer.

- siri.paddu December 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

memmove(void * dest,void * src, int len)
{
if(dest <= src)
{
for(int i=0;i<len;i++)
{
dest[i]=src[i];
}
}
else
{
for(int i=len-1;i>=0;i--)
{
dest[i]=src[i];
}
}

}

//optimisation goes in multiple directions
//1. Using natural arithmetics for 32bit you can do 4 bytes at once
//2. using movement instructions in assembly (DMA?)
//3. paralel computation, but issue is, that bottleneck is RAM acess time.

- Luka Rahne December 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

dest[i] doesn't make sense for a void pointer. Also, len should be unsigned of type size_t.

- Anonymous December 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ya. The solution is a straight reject.
You need to typecast the void pointers to appropriate data types.

- siri.paddu December 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. First see if it's 32-bit or 64-bit machine, and copy 4 or 8 bytes at a time.
2. AFAIK x86 does not support moving from and to memory?
3. Not sure Most of reads/writes will be made to cache so
as long as you copy sequentially going parallel won't help unless
you can specify which core each thread should be scheduled.
But I guess this is worth mentioning to the interviewer.

- anonyguru December 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the code:

void* __memmove__(void* s, const void* d, size_t size) {
  if (s == d)
    return d;

  char* myS = (char*)s;
  char* myD = (char*)d;
  long long direction = 1;
  if (myD < d && (myS + size) > d) {
    myS = myS + size - 1;
    myD = myD + size - 1;
    direction = -1;
  }


  for (long long i = 0; i < size; i++) {
    *myD = *myS;
    myS += direction;
    myD += direction;
  }

  return (void*)myD;
}

One optimization I can think of is casting void* to (long long*) and copy 8 byte at a time. After that, you have to copy the remaining data byte by byte

- orm December 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Buffer overlap needs to be checked.
In addition to suggestions above, copying from back to front is how it is implemented

- Jmincoder December 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void* mymemmove(void* dest, void* source, size_t num)
{
  unsigned char* destc = (unsigned char*)dest;
  unsigned char* sourcec = (unsigned char*)source;
  for ( ; num > 0; --num )
  {
    destc[num-1] = sourcec[num-1];
  }
  return destc;
}

int main()
{
  char* str = (char*)malloc(100);
  memset(str, 0, 100);
  strcpy(str, "memmove can be very useful......");
  mymemmove(str+20, str+15, 11);
  puts(str);
  free(str);
  return 0;
}

- cosmin January 02, 2013 | 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