Google Interview Question Front-end Software Engineers

  • google-interview-questions
    0
    of 0 votes
    7
    Answers

    In C++, write functions for:

    string serialize(vector<string> v);
    vector<string> deserialize(string s);

    such that a string returned from 'serialize' can be passed into deserialize to get the original set of strings back.

    - bjh on August 14, 2010 Report Duplicate | Flag
    Google Front-end Software Engineer C++



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

How about writing them on file using a delimiter but who knows if this delimiter occurs as a character in string. If it may occur we can replace all occurrences of such a delimiter in string with double delimiter just to mark it a escape character so that we ignore occurrence of double delimiter. But this approach would fail if vector contains empty strings as well. We can repeat it more to mark empty strings.

- ashish.agr82 on August 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

first output the vector size(number of strings). Then for each string, output the length of the string, and then the string itself.

- Anonymous on August 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can create a self defined message for serializing

Serialized Byte Array = No. Element (4 bytes) + element(1) + element(2) + .. + element(n)

where each element is represented as
element = Length (4 bytes) + Data (Length Bytes)

This helps us on as we can define the reserve function to avoid resizing of the vector while creating it during deserialization.

- or_knob on August 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Tried and tested following code it works fine.
Let me know if you can find some edge cases when it would not work.

#include<vector>
#include<string>
#include<iostream>
using namespace std;

int serialize(const vector<string> & stringVector1)
{
	FILE *fptr = fopen("C:\\serializedString.txt","w");
	if(!fptr)
		return 0;

	vector<string>::const_iterator i = stringVector1.begin(), end = stringVector1.end();
	for(; i<end;i++ )
	{
		::fputs((*i).c_str(),fptr);
		::fputs("\n",fptr);
	}
	fclose(fptr);
	return 0;
}
int Deserialize(vector<string> & stringVector2)
{
	FILE *fptr = fopen("C:\\serializedString.txt","r");
	if(!fptr)
		return 0;

	char arr[1024],*ptr=NULL;
	while(!feof(fptr))
	{	
		ptr = fgets(arr,1024,fptr);
		if(ptr == arr)
			stringVector2.push_back(string(arr));
	}
	
	fclose(fptr);
	return 0;
}

int main()
{
	vector<string> stringVector1,stringVector2;
	stringVector1.push_back("Cracking");
	stringVector1.push_back("Programming");
	stringVector1.push_back("Interview");
	stringVector1.push_back("with");
	stringVector1.push_back("Arif");
	stringVector1.push_back("and");
	stringVector1.push_back("Krishna");
	serialize(stringVector1);
	Deserialize(stringVector2);
	vector<string>::iterator i = stringVector2.begin(), end = stringVector2.end();
	for(; i<end;i++ )
		cout<<*i;
	getchar();
	return 0;
}

- Crime_Master_GoGo on August 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

While serializing we need to change all the spaces of the strings in the vector with a character set like "%20". After that we can we can get our new formed string where all the independent strings which are differentiated from each other by a space.

While deserializing if we get a space character we form the string and change all the %20 character sets with spaces. The string vector is formed by adding each string to it.

- cac on September 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="c++" line="1" title="CodeMonkey10386" class="run-this">#include <string>
#include <string.h>
#include <vector>
#include <boost/lexical_cast.hpp>
#include <iostream>

using namespace std;
string serialize (vector<string> v) {

string result;
vector<int> lengths;
lengths.resize(v.size());
int sum = 0;
for (int i= 0; i < v.size(); i++) {
lengths[i] = v[i].size(); sum += lengths[i];
}
result += boost::lexical_cast<string>(lengths.size());
result += string(";");
for (int i = 0; i < v.size(); i++) {
result += boost::lexical_cast<string>(v[i].size());
result += string(";");
}
for (int i = 0; i < v.size(); i++) {
result += v[i];
}
cout << result.size() << endl;
return result;
}

vector<string> deserialize (string v) {
vector <int> lengths;
vector <string> output;
int pos = 0;
int seppos = v.find_first_of(';', pos);
int total = boost::lexical_cast<int>(v.substr(pos, seppos-pos));
pos = seppos + 1;
cout << "Total is:" << total << endl;
lengths.resize(total);
output.resize(total);
for (int i = 0; i < total; i++) {
seppos = v.find_first_of(';', pos);
lengths[i] = boost::lexical_cast<int>(v.substr(pos, seppos-pos));
pos = seppos + 1;
cout << "lengths [" << i << "]=" << lengths[i] << endl;
output[i].resize(lengths[i]);
}
for (int i = 0; i < total; i++) {
for (int j = 0; j < lengths[i]; j++) {
output[i][j] = v[pos++];
}
}
return output;
}

int main(int argc, char** argv) {
vector<string> input;
input.resize(argc);
for (int i = 0; i < argc; i++) {
input[i] = string(argv[i]);
}
string output = serialize(input);
cout << output << endl;

vector<string> deserialized = deserialize(output);

for (int i = 0; i < deserialized.size(); i++) {
cout << deserialized[i] << endl;
}
}
</pre><pre title="CodeMonkey10386" input="yes">test testtest testtesttest t </pre>

- Anonymous on September 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think we can construct a delimiter that will consist of string size and some mark-able character, that way any string character will not be able to mimic delimiter.

- Anonymous on December 28, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

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