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;

- Anonymous March 28, 2012 | Flag Reply
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>>>

- saurabh March 28, 2012 | Flag Reply
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!

- Itcecsa March 28, 2012 | Flag Reply
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!

- Itcecsa March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Sapan March 28, 2012 | Flag Reply
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);
}

- Subba March 28, 2012 | Flag Reply
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);
}
}

}}}

- aantrix March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

comments are welcomed

- aantrix March 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- BlackMath April 01, 2012 | Flag
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));
}

- Anonymous March 28, 2012 | Flag Reply
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

- Anil March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous March 29, 2012 | Flag Reply
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");
int m=Console.Read();
for (int x = 1; x < m;x++)
Console.WriteLine("Result : {0}",p.genrateRandom(1, 2,x));
}

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

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

- elbek June 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry this commet for another post. Sorry again

- elbek June 03, 2012 | Flag
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));
    }
}

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

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

- elbek June 03, 2012 | Flag
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

- richa.shrma.iitd April 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous April 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous April 16, 2012 | Flag Reply
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 );

}

- Chad June 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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

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

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

- saurabh March 28, 2012 | 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