Microsoft Interview Question






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

1st option is to create a new array with one occurance of each number.
2nd option is to hold 2 pointers:
and compare the first pounter value with the 2nd pointer value.
if the values are equal increase the 2nd pointer and compare the next value with teh first pointer.
if the value are different and the 1st and the second pointers are more than 1 entry difference, put the second pointer value in the 1st pointer address + 1. and increase the first position address value

Eventually, you will have the original array with one occurance of each value. and the array last value will be first pointer place

- Eila June 13, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

have you considered if it is really necessary to have to pointers?

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

Use maps and insert each element into map. All duplicates will be removed automatically. The aray is not even required to be sorted.

- Aditya February 23, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

give your own solution. Don't use solutions(e.g. map)

- VM November 13, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Or it's implementation:

void unique(int* p, int n){
   int i=0,j=1;
   for(;j<n;++j){
      if(p[i]!=p[j])
         p[++i]=p[j];
   }
}

- zzz100 February 26, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

null exception errors. and you pass in a parameter n for no apparent reason.

- chris March 05, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi,
take two pts current and insert both pointinfg to 2nd element.
if the element pointed by current is not equal to
the element pointed by (insert-1) copy current element
at index insert and increment current and insert
by 1.
otherwise (if both are equal) increment current by 1.




int remove_duplicates(int * p, int size)
{
int current, insert = 1;
for (current=1; current < size; current++)
if (p[current] != p[insert-1])
{
p[insert] = p[current];
current++;
insert++;
} else
current++;

return (insert-1); //return the last index of
//array with unique elements
}

- Ravi Kant Pandey March 26, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sorry ,the above wont work

updated:

remove_duplicates(int * p, int size)
{
int current, insert = 1;
while(current < size){
if (p[current] != p[insert-1]){
p[insert] = p[current];
insert++;
}
current++;
}
return (insert-1); //return the last index of
//array with unique elements
}.

- Ravi Kant Pandey March 28, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

oh missed one thing again.
current should be initialised
current =1
int current, insert = 1; is replaced by
int current=1, insert = 1;

- Ravi Kant Pandey March 28, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my code....

#include<stdio.h>
#include<stdlib.h>
void main()
{
	int a[100],n=0,i, j;
	printf("Enter the total no. of elements in the array\n");
	scanf("%d", &n);
	printf("Enter the elements in sorted order \n");
	for(i=0;i<n;i++)
		scanf("%d", &a[i]);
	j = 0;
	for(i=0;i<n-1;i++)
	{
		if(a[i] > a[i+1])
		{
			printf("Array not sorted \n");
			exit(0);
		}
		while(a[i] == a[i+1])
			i++;
		a[j++] = a[i];
	}
	if(i!=n)
		a[j++] = a[n-1];
	printf("The new array is \n");
	for(i = 0; i<j; i++)
		 printf("%d ", a[i]);
}

- gauravk.18 February 22, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int array[15] = { 1,1,1,2,2,3,3,4,4,4,5,5,6,7,7 }

I assumed that array size is given to us. let suppose it is N

1. start traversing the array from end.
2. compare current and next element if they are equal
then shift the array to next element location.
( Remember shifting of an element doesn't considered in complexity )
3. else cont. the loop

so time complexity O(N)

- Deepak Garg May 27, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//copy the unique items to a new array and then copy it back
	public static void remove_newarray(int [] array){
		int[] a=new int[array.length];
		a[0]=array[0];
		int count=0,j=0;
		for(int i=1;i<array.length&&count<(array.length-1);i++){
			while(count<(array.length-1)&&a[count]==array[i]){
				i++;
			}
			count++;
			a[count]=array[i];
		}
		for(j=0;j<count+1;j++){
			array[j]=a[j];
		}
		while(j<array.length){
			array[j]=0;//use 0 to fill in the extra spaces in the array
			j++;
		}
	}
	//another approach is to use hashtable and then copy back to the array, if the sorted order is not required
	public static void remove_hash(int [] array){
		Hashtable<Integer, Integer> ht=new Hashtable<Integer, Integer>();
		for(int i=0;i<array.length;i++){
			ht.put(array[i], i);
			array[i]=0;//clear the original array
		}
		int count=0;
		for(int t:ht.keySet()){
			array[count]=t;
			count++;
		}
	}
	//use two pointers in the original array
	public static void remove_pointer(int[] array){
		int p1=0,p2=p1+1;
		while(p1<array.length&&p2<array.length){
			while(p1<array.length&&p2<array.length&&array[p2]==array[p1]){
				p2++;
			}
			p1++;
			if(p2<array.length){
			array[p1]=array[p2];
			}
			else
				break;
		}
		System.out.println(p1);
		while(p1<p2){
			array[p1]=0;
			p1++;
		}
	}

- MD March 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class DuplicatesRemover {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int array[] = {1,2,3,3,4,5,5,6,7,7,8,9,9};

int f = 0;
int s = 1;
while(s != array.length){
if(array[f] != array[s]){
array[f+1] = array[s];
f = f+1;
s = s+1;
}
else{
s = s+1;
}
}
f++;
while(f < array.length){

array[f++] = 0;
}

for(int i : array){
System.out.print(i+" ");
}
}

}

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

we can use two pointers as mentioned above or we can stack and check the peek value with the incoming value. If it matches iterate to next value if it doesn't insert into the stack.

- a.varadachari August 26, 2017 | 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