Google Interview Question for SDE1s


Country: United States




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

The issues is you have a huge file.... you do not want to read in the entire file and process every line.

Open the file, print every character until you hit a newline.

Re-Open the file for random-access read, and set the read pointer at the EOF.
read characters backwards until you hit a newline, then print out the characters you've just read (in reverse order).

If you can't read one character at a time, read a block or page or whatever the file system allows, and work backwards in the block buffer, reading previous blocks as necessary to backup and find the preceeding newline.

- reststop November 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

int main() {
	ifstream fin("data.txt");
	string s, e;
	int n = -1;
	char x =  ' ';

	getline(fin, s); // Get First Line

	while (x != '\n') // Set Pointer to end of file, loop until beginning of last line
	{
		fin.seekg(n, std::ios::end);
		fin.get(x);
		n--;
	}
	getline(fin, e); // Get Last Line
}

- TheShocker1999 November 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

I am just guessing. We can use Head and Tail commands to read the first and last lines from a file.

- revanthpobala November 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

you are correct, but you are not allowed to use library functions, so implement/code tail command

- Anonymous November 07, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

load initial 1024 bytes into memory, search newline and print the contents upto newline for the head.
In a while loop keep reading 1024 bytes, until the returned bytes are less than 1024 which means, we are at the end. Print from \n to EOF

- deepan prabhu babu December 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

head -1 filename
tail -1 filename

- Anonymous November 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

head -n 1 file
tail -n 1 file

- Anonymous November 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

head -n 3 game.txt > game1.txt (This commmand gives first 3 lines of txt file)
tail -n 3 game.txt > game2.txt(This command gives last 3 lines of txt file)

- anudeepreddy November 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

head -n 1 game.txt > game1.txt(first line of file)

tail -n 1 game.txt > game2.txt(last line of file)

- anudeepreddy November 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Python 2.7.x code:

with open('index.txt', 'r') as f:
l = f.readlines()
l = [i.replace('\n', '') for i in l]

print l[0]
print l[len(l)-1]

- dimitri.golubev1978 November 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

>>> l = f.readlines()
MemoryError

- Sergey November 06, 2015 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Python 3.4:

with open('giant_file.txt', 'r') as freader:
    line = freader.readline()
    print("first: {}".format(line))
    for line in freader:
        pass
    print("last: {}".format(line))

- purplediane.chen November 14, 2015 | 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