Bloomberg LP Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

int f29() {
       int radix = 2;
       int res;
       do {
       res = f1() * radix^4 + f1() * radix^3 + f1() * radix^2 + f1() * radix^1 + f1();
       } while (res > 29);
       return res;
}

- xuzhiwen1024 June 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Needs to have a while loop

- Anonymous June 29, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What does it return if res == 31 ?

- RecruitingForSandvineCanada June 30, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Also, (not important in this small case), Horner's rule might help optimize

- RecruitingForSandvineCanada June 30, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@RecruitingForSandvineCanada
good suggestion

- xuzhiwen1024 June 30, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code is wrong. ^ does XOR so the biggest value this function can return is 11. Did you want to use pow() function instead?

- Ian August 02, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

Why not use system time? The seconds go from 0-59
f(1)= get the current time and extract the seconds%2.
f(29)= get the current second, f(29)=(second>=29)?second:(second-30)

- Kimi Raikonnen August 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

From Elements of Programming Interviews;

int UniformRandom(int a, int b)
{
	int t = b - a + 1;
	int res;
	do
	{
		res = 0;
		for (int i = 0; (i << 1) < t; i++)
		{
			res = (res * 2) | f1();
		}
	} while (res >= t);
}

return res + a;

- kalkavan June 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int Gen29()
	{
		while(1){
			
			int Res = 0;

			for(int i = 0; i  < 5; ++i){

				Res = Res << 1 | f1();
			}

			if(Res <= 29)
				return (Res);
		}
	}

- jason lee November 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Reservoir sampling seems appropriate solution.

- Eugene January 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

how about calling f1() 29 times and adding them as it would generate 0 to 29 with equal probabolity

- dhruv May 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use bit wise operators

int x = f1();
x = (x << 1 + f1);
x = (x << 1 + f1);
x = (x << 1 + f1);
x = (x << 1 + f1);

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

/// Javascript
 function f29(){
  return (f1() * (29 - 0)) + 0
 }

- vikram January 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int
random (int x)
{

if (x == 2) {
return f1() + f1();
}
return random(x-1) + f1();
}

Solution
Given f1()
Lets build for f2()
f2() is f1() + f1(): which can deliver 0 + 0 or 0+1 or 1+1 or 1+0 (equal probability)

Now build f3()
f3 is f2() + f1() :
the above code works in this fashion

- Anonymous October 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Do it binary search way. Pick any half with equal probability until you finally land at one number. O(log 29)

int left=0
int right = 29


while(left<right){
	
	int mid = (left+right)/2

	if(f1())
		left=mid;
	else
		right=mid;

	if(left==right)
		return left;

		}

- sid July 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Adapting the binomial options pricing model and compound probability we should be able to return the sum of f(1) over 30 trials to give us equal probability in returning [0..29]
The probability is going to be .5^29

public static int f29() {
	int start = 0;
	int randomPick = 0;
	while (start <= 29) {
		randomPick += f1();
	}
	return randomPick;
}

- CodingNoob October 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I forgot to add 1 to start.

public static int f29() {
	int start = 0;
	int randomPick = 0;
	while (start <= 29) {
		randomPick += f1();
		start++;
	}
	return randomPick;
}

- CodingNoob October 06, 2015 | 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