Google on-site IT - 聚贤堂 - 华人职业社交网络

来源:百度文库 编辑:神马文学网 时间:2024/04/20 11:13:15
Given a number, describe an algorithm to find the next number which is prime.
There are 8 stones which are similar except one which is heavier thanthe others. To find it, you are given a pan balance. What is theminimal number of weighing needed to find out the heaviest stone ?
Answer: Divide the stones into sets like (3,3,2). Use pan to weigh(3,3). If the pan is remains balanced, the heavier is in the remaining(2). use pan again to find which is heavy from (2). (Total pan use 2)
If the pan does not remain balanced when weighing (3,3), pick the setof stones that outweigh other. Divide it into (1,1,1). use pan to weighfirst two. It it remains balanced, the remaining stone is the heavier,otherwise the one outweighing other is heavier(total pan use 2)
Order the functions in order of their asymptotic performance
* 2^n
* n^Googol ( 10^100)
* n!
* n^n
Answer: n^(whatever constant), 2^n, n! (==> n^n / e^n ), n^n
what is the best and worst performance time for a hash tree and binary search tree?
Answer: Best is O(c), worst is O(log n)
Questions regarding a string Class
* What do you need when you instantiate a string ( i said a byte array and possibly the length) ?
* What is the best and worst time for the operation ‘equals‘
* How do you spedup the ‘equals‘ operation (i said used hash MD5 for example)
This still has some problem ( a hash can be the same for unequal strings)
-> Use Boyer–Moore string search algorithm => O(n)
Trade offs between a multi threaded and single threaded implementation ?
N threads .. n resources .. what protocol do you use to ensure no deadlocks occur?
There are a set of ‘n‘ integers. Describe an algorithm to find for eachof all its subsets of n-1 integers the product of its integers.
For example, let consider (6, 3, 1, 2). We need to find these products :
6 * 3 * 1 = 18
6 * 3 * 2 = 36
3 * 1 * 2 = 6
6 * 1 * 2 = 12
last one is simple
int mul = 1; for i = 1 to N mul *=a[i];
for i= 1 to N a[i] = mul/a[i];
(I got this question, your answer is not correct) First of all ifa[i]=0 you get an exception, the guy wanted me to use dynamicprogramming to solve this.
Here‘s the dynamic programming solution: You want to build a tablewhere x[i,j] holds the product of (ni .. nj). If you had such a table,you could just output
for (int i = 1; i <= n; i++) {
if (i == 1)
print x[2,n];
else if (i == n)
print x[1,n-1];
else
print x[1,i-1] * x[i+1,n];
}
To build the table, start along the diagonal (x[i,i] = ni). Then dosuccessive diagonals from previous ones (x[j,k] = x[j-1,k] * x[j,k+1]).
More Questions
1. Describe Sorting algorithms and their complexity - Quick sort, Insertion Sort, Merge Sort
2. If you had a million integers how would you sort them and how much memeory would that consume?
Binary tree - requires 1,000,000 integers x 4 bytes = 4 MB + leftpointer and right pointer = 12 MB Insertion sort - can be done in under4 MB
3.Describe an alogrithm to find out if an integer is a square? e.g. 16 is, 15 isn‘t
a) simple answer - start at 2 and divide the integer by each successivevalue. If it divides evenly before you reach half way then it is. b)complex answer after much leading - Do the bitwise AND of n and (n-1).If it is zero then it is a square (I think this is wrong. This actuallytests whether n is a power of 2). No, it almost tests whether n ispower of 2. It falsely proclaims 0 to be a power of 2.
C)
i=2;
while(!NO)
{
if((i*i)==Number)
{
NO;
return True;}
if((i*i)>Number)
{
NO;
return false;}
i++;
}
4. How would you determine if adwords was up and running and serving contextual content ?
5428907816463810488188
Another set of questions
Here are some questions I got on my first interview with Google (slightly altered for NDA reasons).
1 Suppose you have an NxN matrix of positive and negative integers.Write some code that finds the sub-matrix with the maximum sum of itselements.
2 Write some code to reverse a string.
3 Implement division (without using the divide operator, obviously).
4 Write some code to find all permutations of the letters in a particular string.
5 You have to get from point A to point B. You don’t know if you can get there. What would you do?
6 What method would you use to look up a word in a dictionary?
7 Imagine you have a closet full of shirts. It’s very hard to find a shirt. So what can you
do to organize your shirts for easy retrieval?
8 You have eight balls all of the same size. 7 of them weigh the same,and one of them weighs slightly more. How can you fine the ball that isheavier by using a balance and only two weighings?
In person interview
1. In Java, what is the difference between final, finally, and finalize?
2. What is multithreaded programming? What is a deadlock?
3. Write a function (with helper functions if needed) called toExcelthat takes an excel column value (A,B,C,D…AA,AB,AC,… AAA..) and returnsa corresponding integer value (A=1,B=2,… AA=26..).
4. You have a stream of infinite queries (ie: real time Google searchqueries that people are entering). Describe how you would go aboutfinding a good estimate of 1000 samples from this never ending set ofdata and then write code for it.
5. Tree search algorithms. Write BFS and DFS code, explain run time andspace requirements. Modify the code to handle trees with weighted edgesand loops with BFS and DFS, make the code print out path to goal state.
6. You are given a list of numbers. When you reach the end of the listyou will come back to the beginning of the list (a circular list).Write the most efficient algorithm to find the minimum # in this list.Find any given # in the list. The numbers in the list are alwaysincreasing but you don’t know where the circular list begins, ie: 38,40, 55, 89, 6, 13, 20, 23, 36.