Google Interview Question
1,223 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Google:
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (11)
1 of 1 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
0 of 2 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
0 of 2 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
1 of 2 people found this helpful
x+y = (1+...+100)-S1
x^2+y^2 = (1^2+2^2+...+100^2)-S2
Helpful Answer?
Yes |
No
Inappropriate?
0 of 2 people found this helpful
(x + y ) ^ 2 = ( 1 + 2 .... + 100) ^2
Which essentially is same as original sum squared..
You need to copy array to a new 100 size array, where position N is number N in the array. Whichever spots are blank are the missing numbers. Loop once to find missing numbers - O(n)
Helpful Answer?
Yes |
No
Inappropriate?
0 of 1 people found this helpful
(x+y) = (n+1)*n/2 - sum;
then find x from 1 to (x+y)/2;
it becomes question 1.
2O(n)
Helpful Answer?
Yes |
No
Inappropriate?
3 of 3 people found this helpful
we know x = N(N+1)/2 -Sum
If two are missing
xy = N!/P where p is the product of all the numbers in the array.
and
x+y = (N+1)N/2 - S
Solve the two equations to get the answer, no need to use squares of numbers. Simple!!!
Helpful Answer?
Yes |
No
Inappropriate?
z = x+y = n*(n+1)/2-s
w = x*y = n!/p
then solve by using:
x = ( z +/- sqrt(z*z-4*w) )/2
Note that the above equation returns both x and y (substitute z and w to believe it)!
Helpful Answer?
Yes |
No
Inappropriate?
def find_missing_numbers(arr):
N = len(arr)+2
z = N * (N+1) / 2 - sum(arr) # x+y
w = math.factorial(N) / reduce(operator.mul, arr, 1) # x*y
return set([
int(( z + math.sqrt(z*z-4*w) )/2),
int(( z - math.sqrt(z*z-4*w) )/2),
])
Left the sum/prod/factorial in 3 separate loops for readability, but could be combined to one.
Helpful Answer?
Yes |
No
Inappropriate?
1 of 1 people found this helpful
z = x+y = n*(n+1)/2-s
w = x*y = n!/p
We could simplify as something like:
z = x + y = 97
w = x * y = 1482
We eliminate one variable to make it solvable:
y = 97-x
So:
x * (97-x) = 1482
97x - x^2 = 1482
x^2 - 97x + 1482 = 0
Now just substitute the variables in place of the integers:
x^2 - zx + w = 0
And use the quadratic formula to solve for x:
(z +- sqrt(z**2 - 4*1*w)) / (2 * 1)
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up
0 of 2 people found this helpful
by Anonymous: