Google Interview Question for Software Engineers


Country: United States




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

vector<int> GetOverallArray(const vector<vector<int>>& A)
{
	vector<int> result;
	multimap<int, pair<vector<int>::const_iterator, vector<int>::const_iterator>> itr_tail;

	for (const auto& arr : A)
	{
		itr_tail.emplace(arr.front(), make_pair(arr.cbegin(), arr.cend()));
	}

	while (true)
	{
		bool not_process = false;
		auto prev_it = itr_tail.begin();
		if (itr_tail.size() <= 1)
			break;
		if (prev_it != itr_tail.end())
		{
			int cur_val = prev_it->first;
			int count = 0;
			for (auto it = next(prev_it); it != itr_tail.end(); ++it)
			{
				if (cur_val == it->first)
				{
					not_process = true;
					count++;
					auto next_it = next(prev_it->second.first);
					if (next_it != prev_it->second.second)
					{
						itr_tail.emplace(*next_it, make_pair(next_it, prev_it->second.second));
					}
					itr_tail.erase(prev_it);
				}
				else
				{
					if (count > 0)
					{
						auto next_it = next(prev_it->second.first);
						if (next_it != prev_it->second.second)
						{
							itr_tail.emplace(*next_it, make_pair(next_it, prev_it->second.second));
						}
						itr_tail.erase(prev_it);

						result.emplace_back(cur_val);
					}
					count = 0;
					cur_val = it->first;
				}

				prev_it = it;
			}

			if (count > 0)
			{
				auto next_it = next(prev_it->second.first);
				if (next_it != prev_it->second.second)
				{
					itr_tail.emplace(*next_it, make_pair(next_it, prev_it->second.second));
				}
				itr_tail.erase(prev_it);
				result.emplace_back(cur_val);
			}

			if (!not_process)
			{
				prev_it = itr_tail.begin();
				if (prev_it != itr_tail.end())
				{
					auto next_it = next(prev_it->second.first);
					if (next_it != prev_it->second.second)
					{
						itr_tail.emplace(*next_it, make_pair(next_it, prev_it->second.second));
					}
					itr_tail.erase(prev_it);
				}
			}
		}

	}

	return result;
}

int main()
{
	vector<vector<int>> A;
	A.emplace_back(vector<int>{1,3,5});
	A.emplace_back(vector<int>{1, 3, 9});
	A.emplace_back(vector<int>{9, 5});

	vector<int> ret = GetOverallArray(A);

    return 0;
}

- LANorth July 06, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is one of the great example for topological sorting.

1 -->3 -->5

1-->3 -->9

9-->5

After topological sorting 1->3->9->5

- Nagendra July 14, 2019 | 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