Microsoft Interview Question for Software Engineer / Developers


Team: Windows Phone
Country: United States
Interview Type: Phone Interview




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

divide by 8 ..store in bytes_copied
mod by 8 ...store in leftover_bits

perform normal memcpy using bytes at dest_address

new_dest_address = dest_address + bytes_copied ;
new_src_Address = src_address + bytes_copied ;
uint8 mask = (1 << (leftover_bits + 1) - 1;
uint8 maskComplement = ~(mask);

new_dest_address = (*new_src_address & mask ) | (*new_dest_address & maskComplement);

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

on mask creation step, instead of creating mask on every run i used a simple lookup table with prebuilt masks

masks[] = {0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F}

but i was not sure which technique is faster (using lookup table or creating it at runtime)... can anyone comment on it ?

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

I guess look up is better:

new_dest_address = (*new_src_address & masks[leftover_bits-1] ) | (*new_dest_address & ~masks[leftover_bits-1]);

- Anonymous July 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

One of those 'Time vs Space' things. Look-up takes more space, runtime creation takes more time!

- Anonymous July 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

void memcpy(char *src, char *dst, size_t nbit){
while( nbit / 8 > 0 ){
*dst++ = *src++; // Multiples of bytes, so copy byte level
nbit -= 8; // Decrement 1 byte
}
// Take care of the remaining bits
*dst = (*src) & (-1 >>(8-nbit));// Do a bitwise and of the src and left bits
}

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

you are overwriting the whole byte in dst

- Sergei September 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

*dst = (*src) & (-1 >>(8-nbit)); should be rewritten as *dst = (*src) & (-1 <<(8-nbit));

- poorvisha April 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you need

*dst = (*dst&~mask) | (*src&mask) where mask = -1>>(8-nbits)

to preserve ~mask bits in *dst and overwrite mask bits.

- anon October 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if we just do a bitwise "or" operation that is similar to copy right ?

- Vijay March 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how to check the "dst" do not locate at the end of memory? or if "dst" locates at somewhere that should be protect?

- xbingo March 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

callers of memcpy have to allocate memory before calling the function or else it will seg fault. The C memcpy behaves like this. If no or insufficient memory is allocated, it will segfault or corrupt other data structures.

- Santosh March 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

oid memcpy(char *src, char *dst, size_t nbit){
while( nbit / 8 > 0 ){
*dst++ = *src++; // Multiples of bytes, so copy byte level
nbit -= 8; // Decrement 1 byte
}
// Take care of the remaining bits
*dst = (*src) & (1 >>(8-nbit)-1);// Do a bitwise and of the src and left bits
}

- Anonymous August 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static unsafe void memcpy(byte* src, byte* dest, int count)
        {
            if (count == 0) return;
            count -= 8;
            while (count > 0)
            {

                *dest++ = *src++;
                count -= 8;                    
            }
            
            byte t = *src;                 
            t = (byte)(t >> ( - count));
            t = (byte)(t << (- count));
            *dest++ = t;
            count = 0;
            return;
        }

- CoquitlamSDU July 24, 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