## Hewlett Packard Interview Question for Software Engineer / Developers

• 0
of 0 votes

Country: India
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
2
of 6 vote

i think it should be sum-(1+99)*99/2

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

this solution is for "if one number is missing"

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

XOR all numbers in the array and all numbers from 1 to 99.

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

Can you please bit more clear XOR all 100 numbers...?

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

``````int FindRepeated (int *a, int n) {

int Repeated= 0;

for (int i = 0; i < n; i++) {
Repeated= Repeated ^ a[i];
Repeated = Repeated ^ (i+1);
}
// Repeated = 1 ^ 2 ^ ... ^ n ^ a[0] ^ a[1] ^ ...^ a[n-1]

return Repeated;
}``````

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

a small correction:

int FindRepeated (int *a, int n) {

int Repeated= 0;

for (int i = 0; i < n; i++) {
Repeated= Repeated ^ a[i];
if(i < (n-1))
Repeated = Repeated ^ (i+1);
}
// Repeated = 1 ^ 2 ^ ... ^ n ^ a[0] ^ a[1] ^ ...^ a[n-1]

return Repeated;
}

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

Here is one of the correct solution

``````int FindMyRepeat(int* a, int n)
{
int actualSum = (n*(n+1))/2;
int actualProd = 1;
int realSum = 0;
int realProd = 1;
for(int i=1;i<=n;i++)
{
actualProd = actualProd*i;
realSum = realSum + a[i-1];
realProd = realProd*a[i-1];
}
int diff_sum = actualSum - realSum;
printf("Diff = %d\n",diff_sum);
printf("ActualProd = %d\n",actualProd);
printf("RealProd = %d\n",realProd);
int repeat = (diff_sum * realProd)/(actualProd-realProd);
return repeat;
}``````

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

its fine...but what if we are asked to find index of the repeated elements.

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

``````int cntSum = 0;  //this will keep the sum from 1 .. 99
int arrSum = 0; //this will keep the array sum
for(int i = 0; i<100 ;i++)
{
arrSum += arr[i];
if(i != 99)
cntSum += i+1;
}
cout<<"Repeated element: " <<arrSum - cntSum<<endl;``````

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

you are calculating 99! (factorial 99). That would be a very huge number.

You will soon find your solution not scalable if you had nos between 1 and 9999.

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

//The sum of numbers from 1..99 = 99*(100/2) = 4950

int arrSum = 0;
for(int i = 0; i<100 ;i++)
{
arrSum += arr[i];
}
cout<<"Repeated element: " <<arrSum - 4950<<endl;

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

@Ayad:This is nt working dude....check if array size 5...i.e n=5.....and array elements are 1 2 2 3 4..it should return 2....but according 2 ur logic it returns 3.

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

@hasinamjanu: u really need to work out ur maths. Ayad solution will work absolutely fine. for array of size 5, n will be equal to 4 and not 5.

@Test: Xoring doesn't require the numbers to be in order. the example you took is wrong.in your example you missed the number 4. the elements should be 1, 2, 3,3,4,5.

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

Finding the repeated number is not an issue. more importantly finding its index efficiently is an issue.

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

even index also can be found...but for this case we have to use an array..space complexity will be 0(n).

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

Not exactly. Once we find the duplicate number then we just need to search it in the array. linear search will do the work in O(n) time and no extra space.

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

Classic interview question, XOR solution.
stackoverflow.com/questions/2605766/how-to-find-a-duplicate-element-in-an-array-of-shuffled-consecutive-integers

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

what about finding the number of clears in an integer array.
For eg: if array is 1 -3 4 2 3 5... -3 and 3 is one clear.. how to count the number of clears like this.

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

``````int a,b[100]; //fill in values b[100]
sum=0;
int num;
a=(99(99+1))/2
int i=0;
for(i=0;i<100;i++)
{
sum=sum+b[i];
}
num=sum-a;
printf("the repeated number is %d", num);``````

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

(1+99)*99/2-sum

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

this solution is for "if one number is missing"

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

yes it is sum-(1+99)*99/2

but we cant find the index of the element.

if we need Index of the element then we should to bindery search.

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

XOR all numbers in the array and all numbers from 1 to 99.

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

@Arja: A XOR A = 0, and
A XOR A XOR A = A
therefore XORing all the elements and 1 to 99 only leaves the repeated number

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

Your solution is wrong. I think you are assuming all numbers are in increasing order. In Question, they specified as "random".
Lets take numbers are in increasing order. ex: 1,2,3,3,5. Your solution gives, 7 as answer, which is wrong.

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