Interview Question






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

Assume that X repeats once, Y repeats 3 times and each of the rest repeats twice.

Let S = XOR of all elements in the array, thus S = X XOR Y

Since X != Y, S != 0. Hence there exists some integer i such that the i-th of S is 1, which indicates that the i-th bit of X != the i-th bit of Y.

Let S1 = XOR sum of all elements in the array whose the i-th bit equal to 0.
Let S2 = XOR sum of all elements in the array whose the i-th bit equal to 1.

{S1, S2} = {X, Y} are two numbers that we're looking for.

Complexity: O(m+n+m) = O(m+n) where m is the size of the array, n is the bit-length of elements in the array, which is small. So basically, it's O(m).

- Anonymous March 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you explain this with an example! It would be more helpful.

- Anonymous March 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a duplicate question and was already discussed b4. Plz search if a question already exist before posting it!

- Anonymous March 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its impossible to search for duplicates before posting. If you find a duplicate, better report duplicate to admin.

- Mahesh March 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

u r right

- anonymous October 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for (int i = 0; i < len; i++)
{
SecIndex = 0;
for (int j = i + 1; j < len; j++)
{
if ((input[i] ^ input[j]) == 0)
{
if (SecIndex == 0)
SecIndex = j;
else
return input[i];
}
}
}

- Tarun+SDE March 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For solving the above algorithm we have to find the element with ocurrance 1 for this we have to add the last bit of every integer in the array and then, we will check if the & operation with the last bit of the integer is 1 then we will increment the sum and then at every step we will check if the sum of all integers is not divisible with 3 if yes then that number will be non repeating in the array.

Implementation:

{int findnonrepeating(int n){
int sum;
int result = 0;
for(int i = 0; i < 32; i++){
	sum = 0;
	int x = (1<<i);
	for(int j = 0; j <n; j++){
		if(arr[j]&x)
			sum++;
	}
	if(sum%3)
		result = result | x;
}
return result;

- swapnilkant11 July 22, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

XOR all the numbers in the array o(n). This will result in XOR of the number repeated once and thrice. Because the XOR of numbers repeated twice would have cancelled each other and XOR of number thats repeated 3 times would result in number itself.
Now XOR the obtained result with each element in the list again. if the result is zero search the array to find if that no is the one that appears thrice or once.
Overall complexity : O(n)

- Harish December 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oops .. sorry for the above comment. Once we have the XOR of the number repeated thrice and once, to find which one is repeated once and thrice is going to take O(n^2)

- Harish December 28, 2011 | Flag


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