Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

XOR all the elements in the array and nos from 1 to 1000. Let x be the element replaced with n. After the operation we will be left with x XOR n, call it y. Since n>1000 , it can be easily determined by another scan ( or can be tracked while XOR operation). Once n is known, x equals n XOR y.

- topgun.in January 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

not work if 2 or more element contain same value....

- Anonymous January 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I assumed that, the array contains all the 1000 nos. of which one is replaced. In case we have duplicates, there won't be a unique answer, because then the only constraint would be that the no. has to be less than or equal to 1000.

- topgun.in January 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How it will work ???? The list is unsorted. x XOR n where x can be any thing from 1-1000, may be i am missing something. Please Expain.

- Aseem Vyas January 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The underlying idea is that XORing a number with itself makes it zero, i.e. a XOR a = 0
e.g. consider an array with elements from 1 to 5 in any order of which a number, say 3 (=x) is replaced with 7(=n). For simplicity let us assume that the nos. in the array are in order (it is not required).
1 2 7 4 5 -> arr[5]
1 2 3 4 5 -> Sequence from 1 to 5

XOR them all and identify n = 7 (>5)
y = 1^2^7^4^5^1^2^3^4^5

Note that, except for 3 and 7 all nos. appears twice in the XOR thus they will end up being zero. Hence the result of XOR is,
y = 7^3

Now, we can compute x as follows,
x = n ^ y
=> x = 7 ^ (7^3)
=> x = 3

- topgun.in January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

even XOR will be of O(n) (time complexity).it would be batter to scan the given array,

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

Another similar way
===
Say Sum1=Sum(1..n) [ This is known frm the formula ]
Say x is missing number
Say n is replaced number >1000 [Scan the array once]
Say Sum2= Sum(Actual numbers in the array) [ This can be calculated]

Sum1-x+n = Sum2
-> x= Sum1+n-Sum2

- Justforfun July 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

1.If you know n.
Use this formula to get the missing number:
num=n-(n2-n1)
where:
sum of elements after replacing=n2
sum before replacing=n1

P.S: I am assuming all numbers are present from 1 to 1000 before replacing.

2.If n is not known:
while iterating through the array for calculating sum with replaced number just check which number is greater than thousand and that will be your n. Then follow step 1.

- Anshul January 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If we can go through the loop and check each number, we do not need anything else!

- Anonymous January 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Anonymous is right. In fact, since the array's unsorted, there's not really any more efficient way to find out.

- eugene.yarovoi January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anonymous
eugene is right since the array is unsorted....just by iterating and getting "n" you can't tell which number was present there originally. So I don't think there is any more efficient way than this.

- Anshul January 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

assuming array contains elements from 1 to 1000
1) Calculate the sum of first 1000 numbers say (sum_of_natural = (n*(n+1))/2)
2) Calculate the sum (say sum_of_array) of the array skipping the element which is > 1000 (the new element)
3) The replaced number is sum_of_natural - sum_of_array

Time Complexity O(n)
Space Complexity : O(1)

- Anonymous January 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if u don't know the value of n??

- mastee February 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you don't know the number then just scan through the array once and you'll know which number is more than 1000

- Abhishek February 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Get the number which is greater than 1000 say it is some n then to get the exact number.. n-(sum of new array - sum of old array);
this gives the exact number

- Lax March 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

topgun ,what a solution hats off...........

- niraj January 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

TopGun ,
Thanks for posting good solution

- Dileep January 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

TopGun ,
Thanks for posting good solution

- Dileep January 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Say Sum1=Sum(1..n) [ This is known frm the formula ]
Say x is missing number
Say n is replaced number >1000 [Scan the array once]
Say Sum2= Sum(Actual numbers in the array) [ This can be calculated]

Sum1-x+n = Sum2
-> x= Sum1+n-Sum2

- Justforfun July 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The above algorithm can be solved using the xor concept of bit manipulation and we need to xor all the numbers from 1 to n and then we have to xor all the elements of the array as well and finally we will xor both of them together to get the element which is replaced in the array and so we will get the most optimized algorithm for the same with no extra space and time complexity of O(n).

Implementation:

int findmissing(int arr[], int n){
    int array_xor = arr[0];
    int number_xor = 1;
    for(int i = 2; i <= n + 1; i++)
        number_xor = number_xor ^ i;
    for(int i = 1; i < n; i++)
        array_xor = array_xor ^ arr[i];
    return number_xor ^ array_xor;
}

- swapnilkant11 July 23, 2019 | 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