Bloomberg LP Interview Question for Software Engineer / Developers






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

sum and sum of squares should give it.

- Anonymous March 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

@Anonymous: Good one dude

- Balaji March 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you elaborate more on this? thanks

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

First, the sequence is not given sorted.So now,

In one single scan of the array,
-find sum of all nos.(sum1)
-find sum of squares of all nos.(sum2)

From AP, total sum of all nos(including missing nos) is n(n+1)/2 & similarly sum of squares of all nos is n(n+1)(2n+1)/6

therefore, x1+x2 = n(n+1)/2 - sum1
x1^2 + x2^2 = n(n+1)(2n+1)/6 - sum2

Two equations, two variables, solution in O(n).

- Anonymous June 09, 2009 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

There is a very slick XOR-based solution. First, reduce this problem to the problem of finding two unique numbers in an array where every other number occurs exactly twice. Just append or prepend all the numbers in the range 1..N to your array. No need to do it in the code, just in your imagination.

To solve this new problem, you run through the whole array and XOR everything. Duplicates cancel out, so you end up with XOR of the two unique numbers.

Here is when things get slick. The tricky part is to notice that you have too much information. A XOR of two numbers does you little good by itself, but if you take just one set bit from it, things change dramatically. The easiest bit to take is the least significant one: x & -x. Note that this XOR is never zero (those numbers are different), so there must be at least one bit set.

Now what you know about this bit is that it is different in those two numbers (by the very definition of XOR). You also know that other numbers come in pairs, so if any of them has this bit set, its pair must also have this bit set, and if one has this bit cleared, the other one must too. This means that if you XOR all the numbers that have this bit set, duplicates cancel out, and whatever remains is the one of the unique numbers that has this bit set (the other one will be filtered out). Having one number and the XOR of both, the other one is calculated easily.

The code:

pair<int, int> findMissing(const vector<int> &numbers) {
	const int n = numbers.size() + 2;
	int xor2 = 0;
	for (auto i : numbers)
		xor2 ^= i;
	for (int i = 1; i <= n; ++i)
		xor2 ^= i;
	int low1 = xor2 & -xor2;
	int a = 0;
	for (auto i : numbers) {
		if (i & low1)
			a ^= i;
	}
	for (int i = 1; i <= n; ++i) {
		if (i & low1)
			a ^= i;
	}
	return make_pair(a, a ^ xor2);
}

- stachenov October 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>

using namespace std;

void findMissing (int a[], int n, int lost[]){
int j=0;
int k=0;
for(int i=1; i<n; i++){
if (i==a[j])
j++;
else
lost[k++]=i;
}
}

void main()
{
int a[5]={1,3,4,6,7};
int lost[2]={0};
findMissing(a,7,lost);
cout<<lost[0]<<":"<<lost[1]<<endl;
}

is that so simple? or I got the question wrong?

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

{
#include <iostream>

using namespace std;

void findMissing (int a[], int n, int lost[]){
int j=0;
int k=0;
for(int i=1; i<n; i++){
if (i==a[j])
j++;
else
lost[k++]=i;
}
}

void main()
{
int a[5]={1,3,4,6,7};
int lost[2]={0};
findMissing(a,7,lost);
cout<<lost[0]<<":"<<lost[1]<<endl;
}
}
is that so simple or I got the question wrong?

- Anonymous April 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sum of the numbers and sum of the squares of the numbers.
Solve the resultant quadratic equation.

- Baski April 21, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What is the quadratic equation that you solve from this?

- Anonymous June 09, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Anonymous
Of course your assumption that numbers are sorted is a big plus to make it a simple problem. Do you think your solution would work if numbers are not sorted?

- Abhinav May 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Say the Unknown numbers are x and y.
x + y = n(n+1)/2 - (sum of input array);
x * y = n!/(product of input array);

2 eqns 2 unknowns. Solve quadratic eqn to get x and y.

- Bandicoot January 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Product? Ridiculous.

Try writing for n = 10,000 and see what happens.

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

XOR, bit, XOR, XOR.

- Anonymous January 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Take an array A of size n.

Mark A[x], if x is found in the given sequence. Now a second pass will give the missing numbers and complexity is O(n).

- Mahesh February 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

XOR for a single number missing...

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

XOR is good for a single number that doesn't have pair but not for this situation

- Anonymous June 08, 2010 | 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