Microsoft Interview Question for Software Engineers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
1
of 1 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
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

refer CTCI book,

String[] ans = new String[k];
int c = 0;
while(!feof(fp))
{
   fread(fp, buf);
   ans[c%k] = buf;
   c++;
  
}

return ans;

- vbslearn April 18, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I just wrote this .. and I believe it is beautiful. From elegance perspective. Hence, sharing.

def tail_function( file_name , n ) {
  last_n_lines = list( [0:n] ) as { null }
  cur_inx = 0 
  lc = 0 
  for ( line : file(file_name) ){
    lc += 1 
    cur_inx %= n
    last_n_lines[cur_inx] = line 
    cur_inx += 1  
  }
  // print back the last_n_lines, imagining a circular buffer
  start_inx =  lc % n  
  
  for ( [start_inx:n] ){ (x = last_n_lines[$]) || x == null || println(x) }
  for ( [0:start_inx] ){ (x = last_n_lines[$]) || x == null || println(x) } 
}

tail_function( @ARGS[0] , @ARGS[1])

- NoOne May 05, 2020 | 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