## Bloomberg LP Interview Question for Software Engineers

• 0

Country: United States
Interview Type: Phone Interview

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

``````int f29() {
int res;
do {
} while (res > 29);
return res;
}``````

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

Needs to have a while loop

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

What does it return if res == 31 ?

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

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

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

good suggestion

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

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

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)

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;``````

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);
}
}``````

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

Reservoir sampling seems appropriate solution.

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

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);

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

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

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

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;

}``````

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;
}``````

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

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;
}``````

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.

### 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.