NVIDIA Interview Question for Software Engineer / Developers






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

memalign(size, align)
{
     total_size = size + align
     ptr = malloc(total_size)
     ptr += (char *)ptr + (align - (uint)ptr % align)
     return ptr
}

- chandan.jc November 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how will free() work?

- flipper March 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You need to store the offset. Could do something like this in memalign above:

ptr[-1] = align - (uint)ptr % align;

Then, in the free function, you could do something like this:

free(ptr - ptr[-1];

- manish.baj March 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

void * Allignedmalloc(int size, int allsize)
{
   int totalsize=size+allsize-1;
   void *p1=(void*)malloc(totalsize);
   void **p2;
   if(p1==NULL) return NULL;
   p2=(void**)(((int)p1+allsize-1)&(~(allsize-1)));
   p2[-1]=p1;
   return (void*)p2;
}

void Allignfree(void* p)
{
	free((((void**)p)[-1]));
}

- Charles December 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
void *mem = malloc(1024+16);
void *ptr = ((char *)mem+16) & ~ 0x0F;
}

- Nit April 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Both those '+16's should be +15. And the reason for the extra space is that that is the most the returned pointer will be incremented by. You still need to make sure enough memory was allocated, even after the pointer is advanced.

- Dan June 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

There is a function called posix_memalign(void** memptr, size_t alignment, size_t size) that can do this job. But I think this is not what the interviewer wanted to hear. Also posix_memalign requires that alignment be a power of 2. Somebody could claim this to be a limitation, specially in an interview. ;)

- manish.baj March 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about 2 byte align case? I mean less than 4 bytes alignment case.
I think ((void**)p)[-1]) in free function causes problem.
So, I think safe way is adding size + alignment + sizeof(void*) when we allocate memory.

- Will August 23, 2014 | 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