VMWare Inc Interview Question for Member Technical Staffs


Country: India
Interview Type: In-Person




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

Find what the sum 1 .. n should sum to with the formula n * (n + 1) / 2 (see 'summation' in wikipedia). Now sum the actual array. If I've read the problem right and there's a single, extra number, the actual sum minus the theoretical sum should give you the extra number.

- JeffD May 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this will fail at arrray 1,2,3,4,4

- robert May 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@robert - no, I'm assuming n = 4. 4 * (4+1) / 2 == 10; the sum of the array is 14, 14 - 10 = 4 - the extra number.

But that's too easy a question, maybe they meant eg: 1, 2, 3, 3, 5 or 1, 2, 3, 5, 5, ie that there are n numbers, one of them is a dup of another. I don't think sums help much with this form. I can't think of how to solve this off the top of my head.

- JeffD May 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sum of 1,1,3,4,5 and 1,2,2,4,5 is 14 but different element

- robert May 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

XOR all elements form given set, let say...X
and XOR elements form 1...n,let say..Y
then repeated element is = X(xor)Y
for example set is 1,2,3,4,4
X=1xor2xor3xor4xor4
Y=1xor2xor3xor4
and repeated element is=X(xor)Y=4

- dhamu.31954 May 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

So you assume there exactly n+1 elements ?? How convenient !
I don't think the question implies that assumption.

- gen-y-s May 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

lets say number a is missing from sequence and b is extra

b-a = actual sum - theoretical sum
b^2-a^2 = actual sum of squares - theoretical sum of squares

and b^2-a^2 = (b-a)*(b-a)

- tarun June 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
4
of 4 vote

The best possible answer is this:

As 1..n numbers are there in which one number is repeated, XOR all the numbers with 1..n which cancels out all the numbers from 1..n and gives the repeated number

public int repeated(int[] arr,int n){
	int xor=0;
	for(int i=0;i<arr.length;i++){
		xor^=arr[i];
	}
	for(int i=1;i<=n;i++){
		xor^=i;
	}
	return xor;
}
       
}

Time Complexity: O(n)

- Karthik Vvs May 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can "xor ^=arr[i]" get index of the repeated num ? Could you give a explaination, because I don't think xor operation work.

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

Your solution is to find missing number and not duplicate number.

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

no..this solution works for duplicate number also..lets say we have numbers 2,3,4,1,4,5 with 4 duplicated and the range is from 1..5 .so after xoring all these numbers with 1..5 we get xor=(2^3^4^1^4^5)^(1^2^3^4^5)=4 which is required :)

- Karthik Vvs May 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, this works. Sorry for the downvote earlier. +1.

(Though, "best" is subjective :-)).

- Loler May 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Take 1, 2, 3, 3, 5
1 = 001
2 = 010
3 = 011
4 = 100
5 = 101

Now 1 ^ 2 ^ 3 ^ 3 ^ 5
= 001 ^ 010 ^ 011 ^ 011 ^ 101
= 011 ^ 011 ^ 011 ^ 101
= 000 ^ 011 ^ 101
= 011 ^ 101
= 110 = 6

and 1 ^ 2 ^ 3 ^ 4 ^ 5
= 001 ^ 010 ^ 011 ^ 100 ^ 101
= 011 ^ 011 ^ 100 ^ 101
= 000 ^ 100 ^ 101
= 100 ^ 101
= 001 = 1

Now 110 ^ 001 = 111 = 7...
Is this the repeated or missing number???

- coding.arya May 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

this question assumes that u have all the numbers from 1..n and a repeated number which makes the no.of elements n+1..so ur example is wrong...u might take 1,2,3,3,5,4..(one number is added at end to make no.of elements n+1)..then my algorithm works :)

- Karthik Vvs May 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you are right... :)
I took wrong input...

1^2^...^n^k ^ 1^2^...^n = k

Is this solution take care of missing number also? No!

- coding.arya May 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

no..there is no point of missing number in this question..bcoz there will always be n elements from 1..n and an extra number in the range 1..n..so no missing number cases here..

- Karthik Vvs May 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

You can do this using a HashSet. The boolean add() function can be used to see if an element can be added (ie it is unique). If the function returns false, print out that number.
Time complexity - O(n)
Space complexity - O(1)

- Sunil May 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

Correct. Using HashSet is the best way to do it.
Most of the other answers are wrong because they assume there are n items, but the number of items has nothing to do with the range of values.

- gen-y-s May 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please realize that wrong is subjective when the question is unclear.

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

Nowhere does the question says how many elements there are, so there is no basis to assume there are exactly n+1 elements, especially since this specific case would make the question into a trivial mathematical computation.

- gen-y-s May 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

wont using the hashset make the space complexity O(n) too? it wont be O(1) right?

- Anonymous July 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdlib.h>
 
void printRepeating(int arr[], int size)
{
  int i;
  printf("The repeating elements are: \n");
  for (i = 0; i < size; i++)
  {
    if (arr[abs(arr[i])] >= 0)
      arr[abs(arr[i])] = -arr[abs(arr[i])];
    else
      printf(" %d ", abs(arr[i]));
  }
}
 
int main()
{
  int arr[] = {1, 2, 3, 1, 3, 6, 6};
  int arr_size = sizeof(arr)/sizeof(arr[0]);
  printRepeating(arr, arr_size);
  getchar();
  return 0;
}

- Razz May 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how about { 3, 3 } as input?

- ankit May 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hashmaps:

Scanner scan = new Scanner(System.in);
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
		
int n;
System.out.println("How many numbers?");
n = scan.nextInt();
		
int i = 0, val = 0;
while (i < n)
{
	val = scan.nextInt();
	if (!m.containsKey(val)) m.put(val, 1);
	else
	{
		System.out.println("Double value: " + val);
		return;
	}
	i++;
}

- acgrama May 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

first sort the array then check linearly.
complexity nlogn.

- raihanruhin May 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

XOR all elements form given set, let say...X
and XOR elements form 1...n,let say..Y
then repeated element is = X(xor)Y
for example set is 1,2,3,4,4
X=1xor2xor3xor4xor4
Y=1xor2xor3xor4
and repeated element is=X(xor)Y=4

- dhamu.31954 May 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this will work only when the given set has n+1 numbers....eg. for range 1..5 and set {1,3,3,4} this code will give 2,3,5 as repeating numbers...isn't it???

- bigfish May 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'm confused... if this were a "set" of integers, wouldn't there be no duplicate integers?

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

suppose your array is :

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

then the following code works with time complexity O(n)

Set<Integer> set = new HashSet<Integer>();

for(Integer i : arr) {
if(!set.add(i)) {
System.out.println("This is repeating :: " + i);
}
else set.add(i);
}

- rks May 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

space complexity is also O(N)

- ankit May 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Check this out:

int FindDup(int arr[], int n)
{
	for (int i = 1; i <= n; ++i)
	{
		if (arr[i - 1] != i)
		{
			int temp = arr[i - 1];
			arr[i - 1] = arr[arr[i - 1] - 1];
			arr[arr[i - 1] - 1] = temp;
		}
	}

	for (int i = 1; i <= n; ++i)
	{
		if (arr[i - 1] != i)
		{
			return arr[i - 1];
		}
	}

	return 0;
}

0(n)

- coding.arya May 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This would modify the given array... :P

- coding.arya May 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int findDup (int[] a)
	{
		int result =0;
		int xor1 =0;
		int xor2=0;
		
		for(int i=1; i<a.length; i++)   // Xor all the integers from 1 to n-1
		{
			xor1= xor1^i;
		}
		
		for(int i=0; i<a.length; i++)   // Xor all the integers in the array
		{
			xor2 = xor2^a[i];
		}
		
		result = xor1 ^ xor2;
		
		return result;
		
	}

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

// This was my solution during my Vmware interview.
// where they asked us to remove the repeating elements

removeDuplicates(int arr[], int size, int n){
HashMap<Integer,Integer> hashmap=new HashMap<Integer,Integer>(n);

for(i=0;i<size;i++){
if(hashmap.get(arr[i])==null) hashmap.put(arr[i],1);
else hashmap.put(arr[i], hashmap.get(arr[i]) + 1);
}

leftindex=0;
for(i=0;i<size;i++){
if(hashmap.get(arr[i])>0){
arr[leftindex++]=arr[i];
hashmap.put(arr[i],0); //mark the no. as seen.
}
}
size=leftindex;
}

- ganeshjwhr June 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the question is not clear.Does it mean the repeating number makes the count n+1 or one no. is missing to maintain the count equal to n??

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

Time complexity O(N)
Space factor is reduced by one-eight as opposed to having a temp arr, which is still O(1).
negative integers and all possible repetitions can be found.

#include<stdio.h>
main()
{
  signed int arr[10] = {34,8,234,5,459,234,-1,99,-1,501};
  int checker=0,i,val;
  for (i=0;i<10;i++)
  {
    val=arr[i];
    if(arr[i]<0) val*=-1;
    if((checker & (1<<val))> 0)
      printf("arr[%d] = %d is repeated\n",i,arr[i]);
    checker |= (1<<val);
  }
}

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

Put the numbers in an array in ascending order
Sum of all numbers = x
number of numbers = n
Now the position of repeated number from higher end = (n*(n+1)/2) - x
For example : 1,2,2,3,4
Position from higher end P = 15 - 12 = 3
so repeated number is in 3rd position from higher end so answer is 2
or example : 1,2,3,4,4
Position from higher end P = 15 - 14 = 1
so repeated number is in 1st position from higher end so answer is 4

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

The question is not clear to me.
I can deduce 2 scenarios from the question:
Case 1: One number is repeated and the there are 'n+1' numbers.
Case 2: One number is repeated and another number is missing. So, there are 'n' numbers.

-----------------------

Case 1 Solution:

Calculate sum of available 'n+1' numbers. Let's say it's 's1' .
Calculate sum of (1, 2, ..., n) using formula n*(n+1)/2. Let's say it's 's' .

So, repeated number is (s1 - s)

Time complexity: O(n) i.e. time needed to traverse the array to calculate the sum.

-----------------------

Case 2 Solution:

Let's say that the missing number is 'a' and repeated number is 'b' .

Calculate sum of available 'n' numbers. Let's say it's 's1' .
Calculate sum of (1, 2, ..., n) using formula n*(n+1)/2. Let's say it's 's' .
If 's' is bigger than 's1', then (a - b) = (s - s1), otherwise (b - a) = (s1 - s) .

Calculate sum of squares of available 'n' numbers. Let's say it's 't1' .
Calculate sum of (1^2, 2^2, ..., n^2) using formula n*(n+1)*(2*n+1)/6. Let's say it's 't' .
If 't' is bigger than 't1', then (a^2 - b^2) = (t - t1), otherwise (b^2 - a^2) = (t1 - t) .

Now, (a^2 - b^2) = (a+b)*(a-b)

Hence, solve the two equations to get the missing number and repeated number.

Time complexity: O(n) i.e. time needed to traverse the array to calculate the sums.

- arpmon October 15, 2014 | 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