## Amazon Interview Question for Software Engineer / Developers

Country: United States
Interview Type: Phone Interview

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

First generate a random number between 0 and 1 and add 1 to that result

``````int fraction = (double)rand() / (double)RAND_MAX;
return fraction + 1;``````

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

int random(int min, int max)
{
// x is in [0,1]
double x = rand()/static_cast<double>(RAND_MAX);

// [0,1[ * (max - min) + min is in [min,max]
int that = min + static_cast<int>( x * (max - min) );

return that;
}

call---->random(int min, int max)....<<min=1 max=2>>>

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

Sorry, could have make it more clear. We cannot use rand(), design a algorithm to generate random numbers!

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

Sorry, could have make it more clear. We cannot use rand(), design a algorithm to generate random numbers!

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

( java.util.Date().getTime()/Long.MAX)+1?

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

Have a look...

public static void main(String[] args) {
float START = 1;
float END = 2;
Random random = new Random();
for (int i = 1; i <= 100;i++) {
System.out.println(getRandomFloat(START, END, random));
}
}

private static float getRandomFloat(float start, float end, Random random) {
if (start > end) {
throw new IllegalArgumentException("Start cannot exceed End.");
}
float range = end - start ;
float fraction = range * random.nextFloat();
float randomNumber = fraction + start;
return randomNumber;
}

OR

private static float getRandomFloat2(float start, float end, Random random) {
if (start > end)
throw new IllegalArgumentException("Start cannot exceed End.");
return (((end - start) * random.nextFloat()) + start);
}

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

printf's can be replaced by return statements

{{#include<time.h>
#include<math.h>
#include<stdio.h>
#define MIN 1
#define MAX 2
void main()
{
float a;
short i;
time_t sec1;
sec1=time(NULL);
srand(sec1);
a=log(rand());
if(a>=MIN&&a<=MAX)
printf("%f\n",a);
else
{
if(a>MAX)
{
while(a>MIN) a/=10;
}
if(a<MIN)a+=MIN;
printf("%f\n",a);
}
}

}}}

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

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

I thought somebody said you can not use rand() function.

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

Object test;
for(int i=0;i<10;i++){
test=new Object();
double l=test.toString().hashCode();
l=Math.abs(l)/Math.pow(10,10);
test=null;
System.gc();
System.out.println("random number "+(1+l));
}

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

You might want to use Linear_congruential_generator for random number generation. en.wikipedia.org/wiki/Linear_congruential_generator

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

Get system time t. Answer = 1/t + 1.

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

Program is in C-Sharp:

public double genrateRandom(int min, int max,int x)
{
Random rand = new Random();

return rand.NextDouble() * (max - min) / x + min;
}

static void Main(string[] args)
{
Console.WriteLine("Generating random number between 0 and 1");
Program p = new Program();
Console.WriteLine("Enter a number");
for (int x = 1; x < m;x++)
Console.WriteLine("Result : {0}",p.genrateRandom(1, 2,x));
}

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

good try. U did not use max? so if i give random(1,200), still returning 1.xxxx, below than 2

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

Sorry this commet for another post. Sorry again

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

A java program is here.
System time used.
works fine...

``````import java.lang.Math.*;

public class getRandomNumber
{
public static double random(int min, int max)
{
double l = 0.0;
Long time = System.currentTimeMillis();
time = time%1000;
double t = time*1.0/1000;
l = min*1.0 + t;
return l;
}

public static void main(String args[])
{
System.out.println(random(1,2));
}
}``````

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

good try. U did not use max? so if i give random(1,200), still returning 1.xxxx, below than 2

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

(1000 + rand()%2000 ) / 1000

can do it more better by taking 10000000 instead of 1000, depend upon upto how much precision interviewer wants

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

garimaa.blogspot.in/2012/04/program-19th-in-c.html

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

garimaa.blogspot.in/2012/04/program-19th-in-c_15.html

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

static void rand() {
int num = ((int)(Math.random() * 2) + 1)%3;
System.out.println( num );

}

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

int random12()
{
return (1+rand()%2);
}

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

You function only return 1 or 2....but never no's b/w 1-2....

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.