Microsoft Interview Question for Software Engineers

Country: India
Interview Type: In-Person

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

// C++ program to implement your own tail 
#include <bits/stdc++.h> 
using namespace std; 
#define SIZE 100 
// Utility function to sleep for n seconds 
void sleep(unsigned int n) 
    clock_t goal = n * 1000 + clock(); 
    while (goal > clock()); 
// function to read last n lines from the file 
// at any point without reading the entire file 
void tail(FILE* in, int n) 
    int count = 0;  // To count '\n' characters 
    // unsigned long long pos (stores upto 2^64 – 1 
    // chars) assuming that long long int takes 8  
    // bytes 
    unsigned long long pos; 
    char str[2*SIZE]; 
    // Go to End of file 
    if (fseek(in, 0, SEEK_END)) 
        perror("fseek() failed"); 
        // pos will contain no. of chars in 
        // input file. 
        pos = ftell(in); 
        // search for '\n' characters 
        while (pos) 
            // Move 'pos' away from end of file. 
            if (!fseek(in, --pos, SEEK_SET)) 
                if (fgetc(in) == '\n') 
                    // stop reading when n newlines 
                    // is found 
                    if (count++ == n) 
                perror("fseek() failed"); 
        // print last n lines 
        printf("Printing last %d lines -\n", n); 
        while (fgets(str, sizeof(str), in)) 
            printf("%s", str); 

- Nits August 15, 2019 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


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


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