Microsoft Interview Question for SDE-2s


Country: United States
Interview Type: Phone Interview




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

Use fseek to find the size of file and then read backwards from the end until n line breaks are read and then print the result.
Another solution can be like this:
Take a buffer for n lines and keep updating the buffer while reading from the files. So that at last the buffer contains the last n lines.

- Anand Barnwal June 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I was asked the same question and I answered the same what you have written. He then asked me a follow-up question that how would you optimize this process for a large number of files?

- User181920 August 08, 2019 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

You can use a doubly linked list with a fixed size of N.
InsertLast and RemoveFirst.

In the end, you'll have the last N lines.

- Felipe Cerqueira June 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a Queue with a size limit of n, whenever n is exceed dequeue. Do this through the whole file if reading forward is the only option available.

If there is a way to read the file stream in reverse, then that's the best option.

- 2CunningHam June 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can read a file data in vector and then read n lines from vector in reverse order.

- Swts June 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Store N lines in a vector. When N lines have been read, start adding to the beginning again and increment a counter by 1 % size of the array for each element read.

readNLines() {
	int counter = 0;
	while(file NOT empty) {
		if(vector.size() < n)
			vector.push_back(file.line)
		else {
			vector[(counter+1)%n] = file.line
			counter = (counter+1)%n
		}
	}
	for(int i = 1; (counter+i)%n != counter; ++i) {
	output vector[i-1]
	}
}

- SycophantEve June 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the output shouldn't be vector[i-1] but instead vector[counter+i-1] sorry.

- SycophantEve June 14, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <queue>
#include <fstream>

using namespace std;
void printLastNLines(const std::string& fname, int n);

int main()
{
    std::string fname = "myfile.txt";
    int n=10;
    printLastNLines(fname, n);
    return 0;
}

void printLastNLines(const std::string& fname, int n)
{
    ifstream infile(fname.c_str());
    if (!infile.is_open())
    {
        cout<<"File opening error."<<endl;
        return;
    }
    std::queue<std::string> lines;
    std::string line;
    while (std::getline(infile, line))
    {
        if (lines.size() >= n)
        {
            lines.pop();
        }
        lines.push(line);
    }
    infile.close();

    while(!lines.empty())
    {
        cout<<"line = "<<lines.front()<<endl;
        lines.pop();
    }
}

- jeetscoder August 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Go through all the lines one by one and put each line in a node of a single linked list.
Then, take two pointers, one starting at head, and other starting at a distance of n from head

At the end of one traversal of the linked list, you have the first pointer at a distance of n from the end and second pointer at the end.

Just print the lines

- veerun14 September 14, 2015 | Flag Reply


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