Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

typedef
struct
{
int is_available;
int size;
} MCB, *MCB_P;


char *mem_start_p;
int max_mem;
int allocated_mem; /* this is the memory in use. */
int mcb_count;

char *heap_end;

enum {NEW_MCB=0,NO_MCB,REUSE_MCB};
enum {FREE,IN_USE};
void InitMem(char *ptr, int size_in_bytes)
{
/* store the ptr and size_in_bytes in global variable */

max_mem = size_in_bytes;
mem_start_p = ptr;
mcb_count = 0;
allocated_mem = 0;
heap_end = mem_start_p + size_in_bytes;
memset(mem_start_p,0x00,max_mem);
/* This function is complete :-) */

}

void * myalloc(int elem_size)
{
/* check whether any chunk (allocated before) is free first */

MCB_P p_mcb;
int flag = NO_MCB;
int sz;

p_mcb = (MCB_P)mem_start_p;


sz = sizeof(MCB);

if( (elem_size + sz) > (max_mem - (allocated_mem + mcb_count * sz ) ) )
{
return NULL;
}
while( heap_end > ( (char *)p_mcb + elem_size + sz) )
{

if ( p_mcb->is_available == 0)
{

if( p_mcb->size == 0)
{
flag = NEW_MCB;
break;
}
if( p_mcb->size >= (elem_size + sz) )
{
flag = REUSE_MCB;
break;
}
}
p_mcb = (MCB_P) ( (char *)p_mcb + p_mcb->size);


}

if( flag != NO_MCB)
{
p_mcb->is_available = 1;

if( flag == NEW_MCB)
{
p_mcb->size = elem_size + sizeof(MCB); 
}
else if( flag == REUSE_MCB)
{
elem_size = p_mcb->size - sizeof(MCB);
}
mcb_count++;
allocated_mem += elem_size;
return ( (char *) p_mcb + sz);
}

return NULL;


/* if size of the available chunk is equal to greater than required size, use that chunk */


}

void myfree(void *p)
{
/* Mark in MCB that this chunk is free */
MCB_P ptr = (MCB_P)p;
ptr--;

if(ptr->is_available != FREE)
{
mcb_count--;
ptr->is_available = FREE;
allocated_mem -= (ptr->size - sizeof(MCB));
}

}

- saurabh August 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can u please explain the code? If possible, explain it in steps.

- Kiran Kumar August 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

please explain the code...

- alien August 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 4 vote

This question asked by commvault in our college .... Guy's this is not an easy one and even my friends took 1 hr of coding ....(idea is to use array of structs)

- saurabh August 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The fact that you talk about array of structs itself as the main idea means you are not thinking properly.

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

LOL! -100.

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

Why -100 :(

- Leo August 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Two reasons

1) It is an utterly incomplete solution: where is your free implementation? How do you deal with fragementation? What happens when you run out of the array etc?


2) You upvoted yourself.

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

One of the worst answer

- D September 07, 2012 | Flag


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