Microsoft Interview Question for Software Engineers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
0
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"); 
    else
    { 
        // 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) 
                        break; 
            } 
            else
                perror("fseek() failed"); 
        } 
  
        // print last n lines 
        printf("Printing last %d lines -\n", n); 
        while (fgets(str, sizeof(str), in)) 
            printf("%s", str); 
    } 
    printf("\n\n"); 
}

- Nits August 15, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Linux command to read last 10 lines of a file :

tail -n <number_of_lines> /path/to/file

- Sameer August 26, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

create a queue with size 10, start reading the file line by line and as you do so, insert them in the queue, keeping on popping elements from the queue if max size reached. Towards the end you will have last ten lines of the file.

- avm008 October 28, 2019 | 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