Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Take the sizeof(int) operator outside the while loop and put into a varibale. this will improve the performance as it will not calculate size of int each time :).

---Comment if I am wrong

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

"In most cases, sizeof is a compile-time operator, which means that during compilation sizeof expressions get replaced by constant result-values." - wiki

- ultrab April 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wrong. Its compile time constant.

- user32456 June 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Read into some buffer, that would reduce number of roundtrips to file on disk, something like:

#define BUFF_SIZE 100

int readFromFile(char * fileName){
	int i [BUFF_SIZE];
	int readLimit = BUFF_SIZE;

	long sum=0;

	ifstream file(fileName, ios::in|ios::binary);
	if(file.is_open())
	{
		while(!file.eof()) {
			file.read(reinterpret_cast<char*>(i), sizeof(unsigned int) * BUFF_SIZE);
			for(int w=0; w<file.gcount()*sizeof(char)/sizeof(unsigned int); i++)
				sum += i;
		}
	}
	file.close();
	return sum;
}

- theGhost September 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A major cost in reading the file comes from the time to read the disk. To optimize this, one can prefetch blocks ahead of using them. The 4K buffer read is a kind of prefetch, but I would prefetch more and also before computing the sum.

A second possibility is to take advantage of concurrency. We can have multiple threads so that the sum computation can happen in parallel to the io read. We can have multiple threads for io and computation, if there is a possibility of parallelism on the box.

A third possibility is: do we need the "i=0" statement? Isn't i overwritten anyways? This may be a language aspect that I am not familiar with.

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

your answer right. but second possibility i.e. thread solution is wrong

- siva.sai.2020 May 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Without buffering : inefficient code
try{
File f = new File("myFile.txt");
FileInputStream fis = new FileInputStream(f); int count = 0;
int b = 0;
while((b = fis.read()) != -1){
if(b== '\n') {
count ;
}
}
// fis should be closed in a finally block.
fis.close() ;
}
catch(IOException io){}





With Buffering: yields better performance
try{
File f = new File("myFile.txt");
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis); int count = 0;
int b = 0 ;
while((b = bis.read()) != -1){
if(b== '\n') {
count ;
}
}
//bis should be closed in a finally block.
bis.close() ;
}
catch(IOException io){}

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

I have two inputs here for optimization
1) sizeof(unsigned int) , is the culprit, assign the value to an integer and use it.
don't call function in a loop.
2) instead of reinterpret_cast<char*> , if you know that all data read is going to be char*.. just use an explicit cast as (char*)&i.. This saves lot of time as well.

- Vijay Rajanna April 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In most of cases, sizeof calculation takes place in compile time.
And replaced with constant value before compilation.
I don't think it will incur overhead in most cases

- Chun-Fu Chao April 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In most of cases, sizeof calculation takes place in compile time.
And replaced with constant value before compilation.
I don't think it will incur overhead in most cases

- Chun-Fu Chao April 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) get rid of the size of operator
2) instead of reading one integer at time read a specified set of integers .Disk I/O always takes time .

- Tigerthepuli May 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Dont you know the size of file before you start reading.
You might not even need to do interpret cast.
Read in multiples of 4 bytes say 64/128 and do the incremental sum in lopp.
There is no C++ specific stuff here.

- Manish August 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If it's in binary (as opposed to ASCII) and the entire file is nothing BUT integers, I think this could work - we're using a vector in particular since once we get the size of the file, we know exactly what size to make it and only need one allocation (constant resizing for a vector is terrible, so we want to do this without push_back)

long sum=0;

ifstream file("binary.dat", ios::in|ios::binary);
if(file.is_open())
{

file.seekg(file.end, 0);
int numIntegers = file.tellg() / sizeof(int);
file.seekg(file.beg, 0);

vector<int> intVec;
intVec.resize(numIntegers);
file.read(reinterpret_cast<char*>(intVec.data()), numIntegers * sizeof(int)); //now that vector is big enough and we know the file size in advance after using tellg, just read everything in one read call

for(int i = 0; i < intVec.size(); ++i)
sum += intVec[i];

}
file.close();

- Tyler March 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 5 vote

Here is my answer to this question. Comments are welcome.

basicalgos.blogspot.com/2012/04/41-optimize-file-reading.html

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

Please post solution here and stop peddling your blog.

- Anonymous April 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

True man..all u think is increasing number of hits on your blog

- Man !!! April 16, 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