Akamai Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

We can do it by bit logic (E,g, a=4 b=10)
1. Calculate difference b-a (for given e.g. 6)
2. Now calculate ceil(log(b-a+1)(Base 2)) i.e. no of bits required to represent all numbers b/w a and b
3. now call random(0,1) for each bit. (for given example range will be b/w 000 - 111)
4. do step 3 till the number(say num) is less than or equal to 110 i.e. we need only 7 levels since b-a is 6.
5. return num+a.

- loveCoding January 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

modified an answer a lot:

a+(b-a)-(random()*(rand()%(b-a+1))

where rand() is built in function and random is user defined

test case:
a=100
b=250

answer:100+150-0 =250 if random is zero
if random is 1 ans is 100+150 -(1*120)=130
120 generated by built in rand
min will be 100 and max will be 250

- Kaushik February 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if we are asked not to use the built in random() function.

- afs February 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It should be ceil(log(b-a+1)), but that's a minor implementation bug. The general approach is correct, and is the only correct solution on this page.

- eugene.yarovoi February 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How abt this....?
int newrandom(int a,int b)
{ int sum = 0;
for(int i = 0; i <= b-a; i++)
sum+=random();

return (a+sum);
}

- OhMyGod July 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How abt this....?
int newrandom(int a,int b)
{ int sum = 0;
for(int i = 0; i <= b-a; i++)
sum+=random();

return (a+sum);
}

- OhMyGod July 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@OhMyGod: No. The sum of random variables distributed uniformly at random is not itself distributed uniformly at random (by the central limit theorem).

- eugene.yarovoi July 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

The binary-search idea is leading to this:
Split the interval of [a, b] into two half and use the first-half if rand() gives 0 and use the 2nd half if it gives 1. Continue this until the size of the interval is 1, and this will be the random number.

Even the splitting (when the size is odd) must use rand() on which mid to use (lower or upper-mid), otherwise (e.g. using always the lower-mid) leads to wrong distribution.

An even better solution is to use real numbers when splitting - until the interval contains only one integer (size of interval becomes less than 2)

- Selmeczy, Péter February 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The first solution's not correct. See my comments on the other binary search solution posted here. The second solution might actually be OK. Like Manan's solution, it's not guaranteed to terminate after any fixed number of iterations, a property that every correct solution has. It does seem to be truly random if implemented correctly.

- eugene.yarovoi February 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I honestly do not understand how this is not terminating when the size of the interval gets smaller and smaller at each and every step. Can you give an example?

- Selmeczy, Péter February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Peter: Actually I misunderstood your approach. So maybe it does terminate in a fixed number of steps, in which case it's not possible for it to be correct for general a, b.

- eugene.yarovoi September 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class RandomFunctionEx {
public static void main(String[] args) {
System.out.println(random(4, 8));
}

static int random(int a, int b){
if(b < a)
return -1;
int diff = b - a;
int bits = (int) Math.ceil(Math.log(b-a+1)/Math.log(2));
int sum = findRandom(bits, diff);
return a+sum;
}
static int rand(){
return Math.random() > .5 ? 1 : 0;
}

static int findRandom(int numBits, int diff){
int num = 0;
for(int i = 0 ; i < numBits ; i++){
int x = rand() << i;
num = num | x;
}

if(num > diff){
System.out.println("calling again " + num);
num = findRandom(numBits, diff);
}
return num;
}
}

- Anonymous April 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

int random(int a, int b)
{
return(a+rand()/(b-a));
}
//it will return a if rand() generates 0
//it will return b if rand() generates (b-a)*(b-a)

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

Even assuming you meant * instead of /, the question seems to be to generate an integer in the range [a, b] inclusive.

- eugene.yarovoi January 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Random( ) - generates either a 0 or a 1.

- ksandeep.v2 January 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

int random(int a, int b)
{
return(a+rand()%(b-a+1));
}

- Ali_BABA January 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nope. rand() returns either a 0 or 1, so for most ranges, taking that modulo something else wouldn't even have any effect.

- eugene.yarovoi February 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do something like binary search...

int random (int [] arr, int len)
{
  if (len > 0)
    return arr[random_util(arr, 0, len-1)];
  else 
    ERROR
}

int random_util (int [] arr, int first, int last) 
{
  if (first == last) return first;
  int mid = (first + last)/2;

  if (rand()) {
    return (random_util(arr, mid+1, last));
  } else {
    return (random_util(arr, first, mid));
  }
}

- Ashish February 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I knew this solution had to be wrong the moment I saw it because it doesn't have the property of potentially looping an unbounded number of times, a property every correct solution must have. It took me a while to figure out exactly where it's wrong, as the solution does seem very compelling.

The issue is with the treatment of elements when middle elements are present - it's not correct to include the middle with 1/2 probability! Consider the case of [1 2 3]. Your approach gives 37.5% chance for 1 to be chosen instead of 33.3%. Close, but no cigar.

- eugene.yarovoi February 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do something like binary search...

int random (int [] arr, int len)
{
  if (len > 0)
    return arr[random_util(arr, 0, len-1)];
  else 
    ERROR
}

int random_util (int [] arr, int first, int last) 
{
  if (first == last) return first;
  int mid = (first + last)/2;

  if (rand()) {
    return (random_util(arr, mid+1, last));
  } else {
    return (random_util(arr, first, mid));
  }
}

- Ashish February 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do something like binary search...

int random (int [] arr, int len)
{
  if (len > 0)
    return arr[random_util(arr, 0, len-1)];
  else 
    ERROR
}

int random_util (int [] arr, int first, int last) 
{
  if (first == last) return first;
  int mid = (first + last)/2;

  if (rand()) {
    return (random_util(arr, mid+1, last));
  } else {
    return (random_util(arr, first, mid));
  }
}

- Ashish February 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

lets b>a
ra = a(1+rand()) //ra >= a
rb = b(1+rand()) //rb >= b

res = (ra+rb)%(b+1)

res would be a when rand() will give 0 //lower limit
res would be b when ra+rb will give b //upper limit
and for others it'll work fine

- skg February 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

very intuitive and intelligent solution.. good work man.

- abhimanyu February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Take b=10 and a=6 (Like the above solution). If both the rand() calls give 0, we get ra=6 and rb=10. So res=16%11=5. The range of numbers expected is 6 to 10.

- Counter example February 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

a + round(rand()*(b-a+1))

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

That might be close to correct (but not exactly so) if rand() returned a random double from 0 to 1, but rand() returns a random bit; that is, EITHER 0 or 1.

- eugene.yarovoi February 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

algo -
1) Approximate ( b-a ) to the nearest power of 2 .
say b = 3000 , a = 1700 diff = 1300 = 1024 (10 bits ) + 256 (8 bits ) + 16 + 4 .

now generate random numbers within 1024 , 256 , 16 , 4 . ( say w,x,y,z) .

so the random number is a + ( w + x + y + z ) .

- JD February 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This doesn't result in a uniformly random distribution. Because of the central limit theorem, whenever you start summing random numbers, the distribution of the sum is no longer uniformly random.

- eugene.yarovoi February 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

modified an answer a lot:

a+(b-a)-(random()*(rand()%(b-a+1))

where rand() is built in function and random is user defined

test case:
a=100
b=250

answer:100+150-0 =250 if random is zero
if random is 1 ans is 100+150 -(1*120)=130
120 generated by built in rand
min will be 100 and max will be 250

- Kaushik February 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Can u please explain the Answers

- yahoo January 31, 2012 | 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