## 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){
}
}

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];
}
for (int i = 0; i <= lastNonRepeated; i++){
}
}``````

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

Neat solutions.

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

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

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

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

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

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

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``````

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

what does duplicate mean here

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

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}.

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.

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]))
{
Console.WriteLine(arr[i]);
}

}

}

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]))
{
}
}

return result.ToArray();
}``````

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

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

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

It is for the first/screening round.

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.

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.

### 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.