Facebook Interview Question




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

Here's my solutions to this (C++)

string laceStrings(const string& s1, const string& s2)
{
	if (s1.empty())
		return s2;
	if (s2.empty())
		return s1;

	string laced;
	laced.resize(s1.size() + s2.size());

	int i = 0, pos = 0;
	int size = std::min(s1.size(), s2.size());

	// interlace common lengths substrings
	for ( ; i < size; ++i)
	{
		laced[pos++] = s1[i];
		laced[pos++] = s2[i];
	}

	// append the remainder of longer string
	if (s1.size() >= s2.size())
		for ( ; i < s1.size(); ++i)
			laced[pos++] = s1[i];
	else
		for ( ; i < s2.size(); ++i)
			laced[pos++] = s2[i];

	return laced;
}

- V November 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

std::string lace(const std::string& s1, const std::string& s2){
std::string laced(s1.size()<s2.size()?(s1+s2):(s2+s1));
std::size_t pos(0);
for (std::size_t ii=0;ii<std::min(s1.size(),s2.size());++ii){
	laced[pos++]=s1[ii];
	laced[pos++]=s2[ii];
}
return laced;
}

- Vlad November 11, 2012 | 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