## Amazon Interview Question for Developer Program Engineers

Team: Love Film
Country: UK
Interview Type: Phone Interview

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

I think the file can't be loaded to memory at one place because of its size, so approaches goes like as follow.

1. Break the single file into multiple files(let it be M number of files) and each files have K words/lines.
2. Call random(), whch will generate num in between 0 & 1, let it be R.
3. Multiply R by M to get file, then again multiply R by K to get the word from the file.

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

3. Multiply R by M to get file, then again multiply R by K to get the word from the file.
Let T be total words.
Formula = R*T/M(file number) + R*T%M (word number)

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

" think the file can't be loaded to memory at one place"
You don't have to load the whole file to memory to read a specific line.

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

It should be calculated as simply as the following, given totalLines:
Random r = new Random();
double rGen = r.nextDouble();
int lineNum = (int) (Math.round(rGen * (totalLines - 1))) + 1;
return lineNum;

An integer can hold ~ 2^31 (2 billion line numbers), the double rGen can hold even more, so the million line numbers can be accessed using int for line number and double precision for random numbers.

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

@famee How do you know the totalPages value ?

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

@Satya - yes the above approach is wrong

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

or you could do like this
if number of lines is -x
create x/2 number of files so that each file has two words 0 represents the first and 1 represents the last
now the random number generated is multiplied the respective file(M) in which you have the desired word if that is equal to 0 then it represents the word 2*M
if not equal to 0 then it represents the word (2*M)+1

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.

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

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