Netflix Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
'Should not be same'? What if it has 2 bytes, percentage is 50% and you call it a 100 times?
Perhaps you mean it should be 'random'?
I agree. 'should not be same' can be achieved through randomness. I gave the interviewer the same answer. I told him that I would have a helper function within the function to give me a list of random numbers.
I forgot to mention in the question that he asked me assume that the file is all numbers. Each line would be some number.
read the number of letters in the file and find the given percent of the counted number of letters, print the same. If the file is to be called multiple times, store output of calculation of given pecent of the counted number of letters and read the number of letters again starting from the last printed letter(which is the outcome of the given percent calculation on number of letters). find the given percent of the counted number of letters. This particular process can be written inside a loop.
1. Say, percent is given input from user.
2. Read 100 bytes from file
3. generate a random no. between 0 and 100-percent
4. Print percent bytes from randdom no.
5. Repeat this process untill you exhaust complete file.
6. In the last, bytes less than 100 will be read, adjust percent and random function accordingly.
void printFromFile(string fileName, int percentage)
{
char buf[100];
ifstream infile( fileName );
while( !infile.eof() )
{
infile.read( buf, 100 );
int count = infile.gcount();
int to_print = (percentage*100)/count;
int start = rand() % (count - to_print + 1);
for( int i = 0; i < to_print; i++ )
cout << buf[start + i];
}
infile.close();
}
Load file data in memory as per it's capacity, say x% of total file.Now keep printing the given percentage of x, and remove the printed data from memory. Keep doing this till you read complete file, and at the end of it you will have read the given percentage.
- OTR August 16, 2013