Google Interview Question for Software Engineer / Developers






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

I think that a good answer is to send the length of the string in bytes (coded for example as a 4 bytes unsigned integer) followed by the list of bytes, one per char.

The receiver reads the first 4 bytes and understand the string length (let says L), then it reads the following L bytes and build the string.

Here we are assuming that the string is ASCII encoded, so we don't need any other information.

Anyway for completeness, we can encode also the string encoding(ASCII, UTF8, UTF16, ISO*, etc...) using for instance an extra byte after the string length field. In this case the reader reads 4 bytes for the length, 1 byte for the encoding and L bytes for the content. Depending on the encoding the receiver can interpret correctly the string content.

- claudio.corsi January 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

With the help of serialization n Data conversion in form of Byte stream u can send data on network .

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

"how do you escape characters in a string"
He probably was looking for some compression/ coding technique and serialization technique. Not sure .. you should have asked more questions, I usually ask more questions because sometimes the answer evolves out of the details of the question

- blue_skin November 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

He was particular about the ability to reproduce the string on the other side. I guess the answer should just be using serializable in java as it takes care of everything. The code should involve in Output and input streams. Any more suggestions are welcome.

- rya November 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

He must have asked , "how to treat escape characters in the string" e.g newline, tab etc. Search serialization in C++ FAQ's. generic method is given there. (google C++ FAQ + serialization)

Google normally doesn't ask API based Question !!

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

Well .. ya .. using huffman coding you can compress and reproduce on the other side.. The advantage of coding and serializing is you will have fewer bits/bytes to transfer, if you only do serialization of strings then it would take more bits/bytes.

- blue_skin November 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The question is: if you send a bunch of bytes over the network, how do you differentiate between the data (string) and the metadata (description of the data like number of bytes to expect), so the receiver can get the data.

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

One answer would be to use messages with header + body.

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

Then we need a handshake before a connection so that the receiver can know character-hoffman code pairing

- JoshMachine November 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

are they talking about serialize the data using xml

- fat0ss November 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In context of socket programming, i would say he added hint of escape character because we stop reading when we encounter one...i am not sure how to send such an information over network now..

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

Take the case of passing a string to printf(). The string is between " (double quotes). And if the string to be printed has a double quote, then you escape it with double quote ".
In a similar way, you can pick a character/byte to represent the start of the string. If that character appears in the string, then escape it (insert the escape character) and send it over.
However, both sides need to know the escape character before hand.

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

use the method getBytes() of the String class to serialize the string
byte b[]=new String().getBytes();

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

I would use bytes too. In the context of serialization, your protocol would have to on agree on:
1) How many bytes to read in (either a byte-count or a sentinel is common)
2) What character each unique byte maps to

Since there are 256 valid characters, we couldn't use a sentinel without using 9 bits to represent the data, since 8 bits would mean we couldn't tell a sentinel apart from the data itself. Might as well use a byte count instead.

On the serialization end of things you would send a number indicating the number of bytes (characters) to read in, followed by each character mapped to its byte representation. On the deserialization end, keep reading bytes and mapping them to characters up to the number given.

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

The poster haven't understood the question at all which means that he hasn't volunteered to ask any question to the interviewer. Goes a long way to say how he would have fared in the interview.

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

Here's your answer: The basic idea is that you need to be able to represent any of the characters in the data while also using those same characters to mark the end of a string. You do this by using an escape sequence. For example, the stream sees "\0" and knows that the string is terminated and a new one comes next. The final problem is then how to represent a backslash in the actual string, which can simply be "\\".

The reason this is important in serialization is because a stream of data is one continuous flow of bytes. There is no notion of new lines or boundaries between objects until you explicitly code such boundaries in there.

Here is an example Pythonic pseudocode implementation:

def write(str, stream):
	for char in str:
		# doesn't check for double back slash, just checks for back slash,
		# since Python has escape sequences too
		if char == '\\':
			stream.write(char) # write an extra backslash
		stream.write(char)
	
	# write escape sequence in stream to indicate end
	stream.write('\\')
	stream.write('0')

- David Ripplinger February 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I guess he was asking you to compress the string! huffman encoding/decoding might be possible anwers

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

no..he was not..I am looking for an answer to serialize the string and pass it over the network..

- rya November 13, 2010 | Flag


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