Google Interview Question for Software Engineers


Country: United States




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

What is meant by interval is covered? Are these coordinates of a triangle and they are asking if the point is inside?

- Denis July 05, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Updated the question with explanation.

- neer.1304 July 07, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct Interval
{
	bool operator<(const Interval& in)
	{
		return left < in.left;
	}

	int left;
	int right;
};

vector<Interval> UnionInverval(const vector<Interval>& A)
{
	vector<Interval> ret;

	if (A.size() == 0) return ret;

	Interval cur(A.front());
	for (int i = 1; i < A.size(); i++)
	{
		if (cur.right >= A[i].left)
		{
			if (A[i].right > cur.right)
				cur.right = A[i].right;
		}
		else
		{
			ret.emplace_back(cur);
			cur = A[i];
		}
	}

	ret.emplace_back(cur);

	return ret;
}

bool IsIntervalCovered(vector<Interval> A, const Interval& a)
{
	sort(A.begin(), A.end());

	bool ret = false;
	vector<Interval> union_A = UnionInverval(A);
	int left = 0, right = union_A.size() - 1;
	while (left <= right)
	{
		int m = left + (right - left) / 2;
		if (union_A[m].left == a.left) {
			ret = a.right <= union_A[m].right;
			break;
		}
		else if (union_A[m].left < a.left) {
			left = m + 1;
			if (m + 1 < union_A.size() && union_A[m + 1].left > a.left)
			{
				ret = a.right <= union_A[m].right;
				break;
			}
		}
		else
		{
			right = m - 1;
			if (m - 1 >= 0 && union_A[m - 1].left <= a.left)
			{
				ret = a.right <= union_A[m - 1].right;
				break;
			}
		}
	}

	return ret;
}

int main()
{
	vector<Interval> A;
	A.emplace_back(Interval{ 2,5 });
	A.emplace_back(Interval{ 5,7 });
	A.emplace_back(Interval{ 1,4 });

	Interval a{ 1, 6 };

	bool ret = IsIntervalCovered(A, a);

	A.clear();
	A.emplace_back(Interval{ 1,4 });
	A.emplace_back(Interval{ 6,7 });
	A.emplace_back(Interval{ 2,5 });

	a = { 2, 6 };
	ret = IsIntervalCovered(A, a);

	a = { 0, 4 };
	ret = IsIntervalCovered(A, a);

	return 0;
}

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

def is_covered(i, L):
    mask = [False for _ in range(0, i[1] - i[0])]
    for it in L:
        for j in range(it[0], it[1]):
            if i[0] <= j < i[1]:
                mask[j - i[0]] = True
    return False if [it for it in mask if not it] else True

- Anonymous July 07, 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