## Google Interview Question for Software Engineers

• 0

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

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

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

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};
}
}``````

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

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;``````

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

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.

### 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.