Microsoft Interview Question for Software Engineer / Developers






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

This is the "Reservoir Sampling" problem. There is an explanation of it here:
code-slim-jim.blogspot.com/2010/06/reservoir-sampling.html

- mattj777 at yahoo September 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I investigated the url that u posted. thank you, the reservoir sampling problem is the exact match and i loved the idea, so clever.
In the linked list case, complexity becomes O(k*n) -In the worst case, every new element is replaced with the last element in the reservoir- That means a linear complexity. Great..

- erman September 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

That's correct! But you should implement something like this using the linked list instead of array as the base elements. Anyway, this won't be so hard.
0. verify if k < N, if not, exit would be a solution. :)
1. create a array of size k and copy the first k elements.
2. while(list not ended) // I don't know N but I know that the list has an end
get a random index from 0 to k - 1;
move current node index positions;
replace the array[index] with the new value from current node.

- S September 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Move current node index positions???
I's say accept the new (k+i)-th value with probability k/(k+i)

- F September 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi... Just think u r given a doubly linklist

function::
for(i=0;i<k;i++)
{

}

- Prashant September 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi... Just think u r given a doubly linklist

function::
ll=head;
for(i=0;i<k;i++)
{
cnt = rand();
cnt = cnt % k;
j=0;
while(j<cnt)
{
if(ll->next != NULL)
{
ll=ll->next;
}
else
ll=ll->prev;
}
arr[i]=ll->value;
}


Every time this function will give you a new set of random nos from linklist :)

- Anonymous September 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please have a look on this statement

while(j<cn)

In this loop value of 'j' is never modified, so will this loop not go into infinite loop?

- Aninymous December 31, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what if it is a singly linked list

- Anonymous September 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Even in singly linked list, use while loop k(incremented starting from zero each time) time. Always choose node position at random place starting from head. Check for tail.

- Anonymous September 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C# version

Node f(Node list)
{
  if(list==null) return null;
  Node p = list;
  int i=0;
  while(true)  
  {
    if(p.next==null)
    {
      return p;
    }

    Random rand = new Random(0,1);
    if(rand.next()<1/(i+1))
    {
      return p;
    }    
    else
    {
      p=p.next;
      i++;
    }
  }
}

- jiangok January 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

vector<int> random(ListNode* head, int k)
{
      vector<int> ans(k);
      while (k-- && head) {
            ans.push_back(head->val);
            head = head->next;
      }
      int i = k, j ;
      while (head) {
              j = rand() % (i+1);
              if (j < k)
                      ans[j] = head->val;
              i++;
      }
      return ans;
       
      
}

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

vector<int> random(ListNode* head, int k)
{
      vector<int> ans(k);
      int i(k), j;
      while (i-- && head) {
            ans.push_back(head->val);
            head = head->next;
      }
      int i = k, j ;
      while (head) {
              j = rand() % (i+1);
              if (j < k)
                      ans[j] = head->val;
              i++;
              head = head->next;
      }
      return ans;
       
      
}

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

@S
Step 1 second half part is not correct. It says any K random numbers. So why are you copying 'first' K numbers from the list to array?

What is if the random number does not occur in first K elements of the list?

- Anonymous September 12, 2010 | 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