Microsoft Interview Question for Software Engineer / Developers






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

The C standard library specifies calls ftell() and fseek() that allow you to save and restore the position in the file. (Note: Microsoft probably has their own bastardized equivalents.) Just store the offset in a static variable.

- Anonymous March 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah I mentioned fseek to him, when you read the entire file, it s something like
f1 = fopen(file)
readone()
{
while(!feof(f1))
f1 = fscanf("%d",something);
}

But about reading one like everytime, readone() is called, I stil dont get!

- Anonymous March 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include <stdio.h>

static char *readone(char *path) {
	static long offset;
	static char line[8192];
	FILE *f = fopen(path, "r");
	char *p;
	fseek(f, offset, SEEK_SET);
	p = fgets(line, sizeof line, f);
	offset = ftell(f);
	fclose(f);
	return p;
}

int main(int argc, char **argv) {
	char *line;
	while((line = readone(argv[1])) != NULL) {
		fputs(line, stdout);
	}
}

- Anonymous March 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It seems to a pretty neat approach. Does anyone sees anything missing here.

- hary March 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@hary. Everything is missing here.

It gives incorrect answer! It has buffer overflow. The first time readone is called, it could seek to a random offset in the file.

In short, it is crap.

- Anonymous March 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If i wrong or missing something please correct me

I agree buffer overflow would be there. But since the question says readoe would read one line at every call, then i am assuming they there is a restriction on the size of each line, because you need to read each line and store in internal buffer. Any API one uses, fscanf, fgets will make use of a buffer string.
As far as the first call i sconcerned it will not seek to a random offset because offset is static and is initialized to 0, so fseek will move you nowhere w.r.t to the begining of the file.
If you still fell its wrong suggest ways to improve this.

- hary March 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

it happens, its all abt keeping ur nerves. Q is simple and instead of algo, I guess intent was to see the design/architect skills.
I hv very little idea of C/C++ lib for readfile

In C# I would use static counter to keep the count of number of lines read
Then some additional checks like EOF case/ExceptionHandling/Empty lines etc.

Keep trying, everyones knows its hard to get thru, but once u get it, its an accomplishment.

Gud Luck

- pk March 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am sorry, I missed to mention, the interviewer wanted me to use fscanf!

- Anonymous March 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the solution if you forget (or don't know about) the fseek.

string readone(char* filename) {
static int ctr=1; // counter to track how many times readone is called.
string buf;
ifstream in(filename);
for(int i=0;i<ctr;i++) {
getline(in, buf);
}
in.close();
ctr++;
return buf;
}

- Yan March 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think what he wanted to see was what is your knowledge for fscanf.
So you need to give it a scanset of not to stop till '\n'

#include <stdio.h>

void readone();

int main(int argn, char **argv)
{
int i;
for (i=0; i<=2; i++){
readone();
}


}


void readone()
{
static int ctr = 0;

FILE *f = fopen("test.txt", "r");
char str[100];
int i;
for (i=0; i<=ctr; i++){
fscanf(f, "%[^\n]\n", str);
}
puts(str);
fclose(f);
ctr++;
}

- NG March 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

fscanf(f, "%[^\n]\n", str);
should have been...
fscanf(f, "%[^\n]s", str); //Read not-until '\n'(newline) char.

- B March 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

which college are u from is MS hiring?

- @peson who posted the question March 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

doesnt fgets function read a line from a file..

i mean read is terminated as soon as it encounters a /n

- Anonymous March 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No

- hary March 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am so sorry to write an incorrect answer.
Answer to your question is yes
Here is the definition from man pages

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer.

What i intended was that you anyhow would be restricted by SIZE chars and so cap on the buffer used must always be there unless we think of a way to avoid this.

- hary March 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Guys, from my Msft interview experience last year, I can safely assure you that using fseek here would definitely land you with a reject. They tend to overlook candidates who use a lot of library functions while operating on strings.

My 2 paisa!

- Anon March 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Guys, Sincerely, the solution to this question can be from very simple C++ to very complex C. I think it depend on the interviewer's preferences. Below is a simple C++ solution using fstream library. You don't need fseek here as fstream keep tracking it for you. In fact, it always does and we often overlook this as we often use Yan's approach all the times.

fstream file;
void readOne()
{
string str;
getline(file,str);
cout << str << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
file.open("Your file path here",std::ios_base::in);
readOne();
readOne();
readOne();


file.close();
string str;
cin >> str;
}

- Noname April 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

FILE *fp; // fp is file pointer

void read (void)
{
if(!feof)
{
fgets(str,len,fp);//it reads file until new line character is read or len-1 char's
//are read ( so keep len very large number ),
//after this str contains first line of file .
// now file pointer points to end of the first line.
fseek(fp,1,SEEK_CUR);// now file pointer points to next line beginning.
// so when ever file is called it will keep moving to next line
cout<<str;
}
}

- Mr perfect August 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

After read, fgets advances to next line. No need to set the offset.

- Sandeep K April 28, 2013 | 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