Google Interview Question for Software Engineer / Developers






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

Use fseek() function in C. Using fseek(), if you pass negative number, then file pointer moves that number of bytes to its left, and if positive number passed, then it shifts that many bytes right.
First reach end of file using fseek(fp,0,SEEK_END) then keep coming 1 char left and appending chars to string end, each time u find a '\n', reverse the string and print it, again make the string empty and do this until required number of lines are not printed :)

- Ankul August 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Though I don't need tail anymore, I know that it does not print lines in reverse order. :(

- tamil February 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ahh. just noted that without the middle 'm', I would have been just a 'tail' all these days. :p

- tamil February 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

generally, use the round-robin % ...the number of lines required is what to be modular-ed.

void tail(FILE *fp, int num_lines)
{
char **buffer = calloc(num_lines, sizeof(char *));
int index = 0;
int i;

for (;;)
{
char *line = NULL;
int len = 0;

if (getline(&line, &len, fp) < 0)
break;

if (buffer[index])
free(buffer[index]);

buffer[index] = line;

index++;
index %= num_lines;
}

for (i = 0; i < num_lines; i++)
puts(buffer[(index + i) % num_lines]);
}

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

what is a UNIX tail command?

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

Cirular array of size n where n stands for last n elements in the tail, shud do it , i guess.

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

Sweet.

- Nix July 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do they really expect you to know the file commands??

- dazzeled2011 December 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I mean this Q is so out of leauge from rest of the Q's that they usually ask? Was this for some specific position or did you have sth related on your resume?

- dazzeled2011 December 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think fseek to the end and then reading char by char from reverse is the correction solution.
Circular array or queue I assume you guys are reading the whole file - if so that's not good. Imagine big file (millions of files). You shouldn't read more than what is really necessary.

- user2013 October 15, 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