Microsoft Interview Question for Software Developers


Country: United States
Interview Type: In-Person




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

O(N), Idea: let's use data structure which avoids duplicates - hashset

public static Integer[] removeDuplicates1(Integer[] arr){
        HashSet<Integer> set = new HashSet<>();
        for (Integer elem : arr){
            set.add(elem);
        }
        Integer[] answer = new Integer[set.size()];
        set.toArray(answer);
        return answer;
}

O(NlogN), Idea: let's sort input and place all unique elements in the beggining

public static Integer[] removeDuplicates2(Integer[] arr){
        if (arr.length == 0){
            return new Integer[0];
        }
        Arrays.sort(arr);
        int lastNonRepeated = 0;
        for (int i =  1; i < arr.length; i++){
            if (arr[i] == arr[lastNonRepeated]){
                continue;
            }
            arr[++lastNonRepeated] = arr[i];
        }
        Integer[] answer = new Integer[lastNonRepeated+1];
        for (int i = 0; i <= lastNonRepeated; i++){
            answer[i] = arr[i];
        }
        return answer;
    }

- GK February 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Neat solutions.

- sush February 27, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static Integer[] removeDuplicates(Integer[] array) {
return new HashSet<Integer>(Arrays.asList(array)).toArray(new Integer[0]);
}

- Anonymous February 28, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In the first approach, how's it O(N) considering the fact that logarithmic insert is done inside the loop applying to each element?

- Anonymous May 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@anonymous, isnt insert in a Hashset O(1) operation (assuming negligible collisions)

- djtrance January 11, 2016 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

def removeDup(arraylist):
	hash = {}

	for i in arraylist:
		if i not in hash:
			hash[i] = 1

	newlist = []
	for j in hash:
		newlist.append(j)
	return newlist

- hiuhchan February 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what does duplicate mean here

- RAMANA February 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

For instance, if you have an array of numbers a[] = {10,2,2,5,6,6}. You would want to remove the duplicates and get an output array as {10,2,5,6}.

- sush March 02, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In C++ you can use std::set<int> to store the values as you scan the array. Then output the values of set.

- Saket Goyal February 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void RemoveDuplicates(int[] arr)
{
int[] updated = new int[arr.Length];
List<int> list = new List<int>();

for(int i=0;i<arr.Length;i++)
{

if (!list.Contains(arr[i]))
{
list.Add(arr[i]);
Console.WriteLine(arr[i]);
}

}

}

- Anonymous February 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C# implementation using HashSet

public int[] RemoveDuplicates(int[] arr)
{
    HashSet<int> result = new HashSet<int>();

    for (int i = 0; i < arr.Length; i++)
    {
        if (!result.Contains(arr[i]))
        {
            result.Add(arr[i]);
        }
    }

    return result.ToArray();
}

- Onat March 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

since when did Microsoft started to ask such straight forward stuff...:)

- HardCode March 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is for the first/screening round.

- sush March 02, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can have a hashmap where key and value will be integer only, and if value exist for a key means that number is already there otherwise just insert the number.
so lookup is o(1) and it will take one pas on array.
Therefor it will take o(n) time.

- sumanjoshi March 20, 2015 | 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