Microsoft Interview Question


Country: India




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

Moore's voting algorithm, seems a pretty standard problem asked in the tech interviews.

- Murali Mohan August 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Standard question. Unexpected answer.

- Anonymous November 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
5
of 5 vote

#include <stdio.h>

int findmajelement(int arr[], int size)
{
    int count = 1, i = 0, m_index= 0;
	for(i = 1; i < size; i++)
	{
		if(arr[m_index] == arr[i])
			count++;
		else
			count--;
		if( count == 0)
		{
			m_index = i;
			count = 1;
		}
	}
	return arr[m_index];
}

void ismajelement(int arr[], int maj_elem, int size)
{
	int i = 0, count = 0;
	for(i = 0; i< size; i++)
	{
		if(arr[i] == maj_elem)
			count++;
	}
	printf("count = %d\n");
	if(count > size/2)
		printf("majority element = %d\n", maj_elem);
	else
		printf("no majority element present\n");
}

int main()
{
	int arr[6] = {2,4,2,2,4};
	int majority_element  = 0;
	majority_element = findmajelement(arr, 6);
	ismajelement(arr, majority_element, 6);
	return 0;
}

- Clair August 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

1. Sort the array 2. pick the n/2th element

- pop_front September 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include <stdio.h>
#include <stdio.h>
int find(int *a ,int n)
{   
	int target = a[0];
	int num = 1;
	int i;
	for( i=1; i< n; i++)
	{
		if(num == 0)
		{
			target = a[i];
			num++;
		}
		else if(target == a[i])
		{
			num++;
		}
		else
		{
			num--;
		}
	}

	return target;
}

int main()
{
	int a[5] = {0,1,2,1,1};
	printf("%d", find(a,5));
	
}

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Problem1
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
             * Find the majority element which occurs more than n/2 times in an array of n size, 
             * which contains duplicate elements in minimum time and space complexity.
             */
            int[] arr = { 2, 2, 2, 3, 4 };

            Dictionary<Int32, Int32> dict = new Dictionary<int, int>();

            int majItem = 0;
            foreach (int item in arr)
            {
                if (dict.ContainsKey(arr[item]))
                {
                    dict[arr[item]] = ++dict[arr[item]];

                    if (dict[arr[item]] > (arr.Length / 2))
                    {
                        majItem = item;
                    }
                }
                else
                {
                    dict[arr[item]] = 1;
                }
            }

            System.Console.WriteLine(majItem.ToString() + " has occurred more than half the length of the array!");
        }

    }
}

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

Oops - forgot the break statement after majItem = item.

- AB August 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about

public void findElement(int[] elements) {
		Moore m = new Moore();
		int candidate = m.findMajority(elements);
		int count = 0;
		for(int i=0; i<elements.length; i++) {
			if(elements[i] == candidate)
				count++;
		}
		
		if(count > (elements.length/2)) {
			System.out.println("Element "+candidate);
		} else {
			System.out.println("No such element");
		}
		
	}

where the class Moore is a vanilla version of the Moore majority algorithm.

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

If the array is an array of ints, why don't we just sort the array and iterate through the sorted array. This is pretty simple cuz in the sorted array, the same ints should all be right next to each other. This takes O(n) time and almost no space.

- chuang24@buffalo.edu September 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This takes at least O(nlogn) time for sorting.

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

And actually if the array is already sorted, you can find whether there is (and which exactly) element in log time. Just need to take median, cause only it can be element with more then n/2 occurrence, then using binary search find left first occurrence of this value, and then check whether on the position shifted to n/2 from the first occurrence the same value.

- Viacheslav September 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

candidate = 0
count = 0
for value in input:
if count == 0:
candidate = value
if candidate == value:
count +=1
else:
count -= 1

- smashit December 06, 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