Google Interview Question for Software Engineers


Country: United States




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

Concat x and y and add an extra number to know where to split the coordinates. Example: f((45,3))=4530 and f((4,53))=4531

- Anonymous September 30, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

won't work! what if a number is greater than 10**10? In that case you need to split at 10th position.

- alphadraco April 26, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If integers are unsigned, just concatenate bits; if they are signed, reserve two bits for signes:

template<typename T>
T f(T x, T y) {
	if constexpr (std::is_signed_v<T>) {
		const bool is_neg_x = (x < 0);
		const bool is_neg_y = (y < 0);

		const T xy = (std::abs(x) << (4 * sizeof(T) - 1)) | std::abs(y);
		return (xy << 2) | (is_neg_x ? 1 : 0) | (is_neg_y ? 2 : 0);
	}
	else
		return (x << (4 * sizeof(T))) | y;
}

template<typename T>
std::pair<T, T> f_inv(T i) {
	if constexpr (std::is_signed_v<T>) {
		const bool is_neg_x = (i & 1);
		const bool is_neg_y = (i & 2);

		constexpr T mask = (T{1} << (4 * sizeof(T) - 1)) - 1;
		const T x = (i >> (4 * sizeof(T) + 1)) & mask;
		const T y = (i >> 2                  ) & mask;

		return {is_neg_x ? -x : x, is_neg_y ? -y : y};
	}
	else {
		constexpr T mask = (T{1} << (4 * sizeof(T))) - 1;
		return {i >> (4 * sizeof(T)), i & mask};
	}
}

- Evg February 26, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if x>=y:
F(x,y) = x^2 + x+ y
else:
F(x,y) = x + y^2

- Dheeraj May 18, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if(x>=y):
		F(z) = x^2 + x + y;
	else:
		F(z) = y^2 + x;

- Dheeraj May 18, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We may use the cantor function to solve this:
en.wikipedia.org/wiki/Pairing_function

- Rajat.nitp July 03, 2020 | 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