Bloomberg LP Interview Question for Financial Software Developers






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

Its the product and not the sum that should be 36

for(i=1;i<=36;i++)
{
//if i is a factor of 36
if(36%i==0)
{
for(j=1;j<=36;j++)
//if j is a factor of 36
if(36%j == 0)
{
for(k=1;k<=36;k++)
//if k is a factor of 36
if(36%k==0)
{
if(i*j*k == 36)
print(i,j,k)
}
}
}
}

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

a slight modification works:
int num=36;
for(i=1;i<=num;i++)
{
if(num%i==0)
{
for(j=i;j<=num/i;j++)

if(num%j == 0)
{
for(k=j;k<=num/(i*j);k++)

if(num%k==0)
{
if( (i*j*k == num))
printf("%d %d %d\n",i,j,k);
}
}
}
}

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

First find the factors of N (36)
Then iterate through the factors
for (i = factor1; i != null; i = i->nextfactor)
for (j = i->nextfactor; j != null; j = j->nextfactor)
for (k = j->nextfactor; k != null; k = k->nextfactor)
if (i * j * k == N)
answer.

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

"list out the possible combination s for the ages"
so, you should check for:
if (i + j + k == N)

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

The code above produces too much repitition. Here is a better way

for (i=1,2,3) // factors of 36 up to cube root of 36
   for (j in factors of 36/i up to sqrt(36/i)
      print (i,j,36/(i*j)

This will produce all the triples in non-decreasing order. Can be easily generalized to other numbers and number-of-factors.

- John Smith November 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Neat!

One improvement: Check for duplicate printing. That is, combination 1, 1, 36 should be considered same as 1, 36, 1.

The unique key could be as simple as i+j+k.
Create an array check of size [36*3] and use it like a bit vector.

- Nix November 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

not really! check 2+2+9 and 1+6+6.

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

Neat!

One improvement: Check for duplicate printing. That is, combination 1, 1, 36 should be considered same as 1, 36, 1.

The unique key could be as simple as i+j+k.
Create an array check of size [36*3] and use it like a bit vector.

- Nix November 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One more improvement to code by Anonymous,

j has to be a factor of (36/i) and k has to be of (36/i*j)

so if loops of internal for loops can be changed,

-----------
for(i=1;i<=36;i++)
{
//if i is a factor of 36
if(36%i==0)
{
for(j=1;j<=36;j++)
//if j is a factor of 36
if((36/i)%j == 0)
{
for(k=1;k<=36;k++)
//if k is a factor of 36
if((36/i*j)%k==0)
{
if(i*j*k == 36)
print(i,j,k)
}
}
}
}
}
---------------

- shiv November 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can modify it to work a little bit faster.
for(j=1;j<=36/i;j++)
.....
for(k=1;k<=36/(i*j);k++)
....

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

I think the solution by Anonymous is nice; nevertheless, the solution provided by John Smith is much better.

It is O(N^2) and not O(N^3) and the code is neat and easily extensible, as he said. Just play with n^th roots in descending order.

Only a small caveat: the second loop should exclude (i) itself. Suppose i =1, j would go from 1 to 36/1 (36), so the code would also produce 1,1,36 which has a repetition. The same problem arises for the combination (3,3,4) which is permitted by the code
(i=3, j>36/3 and j<sqrt(12) therefore j=3 is permitted)

For generalization any loop has to exclude the precedent indexes; anyhow really great solution, I enjoyed it. Thanks, John

- John Doe December 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

there is no duplicate. suppose i=1, then j cannot exceed 6, so only print 1,1,36 asending order. His code is owesome. Why there is so many modification.

- ZhenZhangQD@googlemail.com January 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

here is a O(n^2) solution....and it does not require calculating factors and it works for any target number

i=1;
while(true)
{
	j=i;
	while(true)
	{
		rem = target%(i*j)
		k = target/(i*j)
		
		if(k<j)
			break
		
		if(rem==0)
			print(i, j, k)
		j++
	}
	
	if(k<i*j)
		break
	i++
}

- NC-State January 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The Thing to note here is that not only that we should not allow duplicate combinations like [2,3,6] and [3,6,2] , but also combos like [1,1,36] are not allowed as said in the question.
My logic goes like this:-
1) Find out all the divisors say 'i' of 36 less than the sqrt of 36 i.e. 6
2) Now , for each 'i', compute 'j' and 'k' such that i*j*k=36 and all are distinct

We can find out 'j' too in a similar fashion as 'i' i.e. divisors of 36 less than it's square root.

Following this fashion, we would end up getting i,j and k in the increasing order and it would avoid duplicates as we are considering divisors only upto the square root.

So ultimately we get the age combos as follows:-

1 2 18

1 3 12

1 4 9

2 3 6

- Mandar January 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

[1, 1, 36] is allowd. Repititions of combinations are not allowed.

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

I just checked it out....the answer by Madar is correct.....for each 'i', check for 'j' between 'i+1' to 36; and for j, check for k between 'j+1' to 36......a triple nested loop and the factors have to satisfy i*j*k=36

- Abhishek Kumar February 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1,1,36
1,2,18
1,3,12
1,4,9
1,6,6
2,2,9
2,3,6
2,6,3

Only these age combo are unique, and this set is what interviewer is looking for.

- Nix July 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you forgot 3, 3, 4.. I have a more general solution, mathematical. Easily programmable, check below comment.

- Puran September 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

although I forgot to mention, some of these combinations will make this man a dirty old pig for his wife... for example, 36,1,1 etc :)))

- Nix July 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#1 (1, 1, 36)
#2 (1, 2, 18)
#3 (1, 3, 12)
#4 (1, 4, 9)
#5 (1, 6, 6)
#6 (2, 2, 9)
#7 (2, 3, 6)
#8 (3, 3, 4)

Is there a mathematical way to do this?

- Puran September 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

by that I mean, is there a definite way to prove that the number of unique combinations is 8?

Can someone explain how.. I got the above solution brute force.

- Puran September 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for ( i = 1; i <= sqrt(36); i++) {
if ( 36%i == 0 ) {
for ( j = 2; j <= sqrt(36/i); j++ ) {
if ( 36%(i*j) == 0 )
cout << i << ' ' << j << ' ' << 36/(i*j) << endl;
}
}
}

- Anonymous August 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Ages will be 2,3 and 6.

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

Why not? 1,2,18 ; 1,6,6 ; 1,4,9 and so on.... There are a lot of possible combinations

- Anon November 11, 2009 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

this is knapsack problem

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

Could you explain a little bit more on how you would map this to the knapsack problem?

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

print (34, 1, 1);
print (33, 2, 1);

for (i = 32; i == 15; i--)
for (j = 1, k = 36-i-j; j>k; j++, k--)
print (i, j, k);

- Anonymous November 19, 2009 | 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