Interviewed at Google, here are GOOGLE INTERVIEW QUESTIONS.

ones3k

Banned
Aug 21, 2005
444
0
0
NOTE: I will be using well-known computer science and programming terminology. If you don't know what something means, search the web. I'm posting this because i couldnt find any damned sample questions before my interview.

also NOTE: The interviewers did not describe these problems exactly as i am describing them. I am describing them in a condensed form, so before attempting the problem, take the time to figure out whats going on first. The interviewers described these problems to me verbally and with diagrams.

=========================================================
Question #1) Given k sorted streams where each stream could possibly be infinite in length, describe an efficient algorithm to merge the k streams into a new stream (also in sorted order).

Question #2) Given a set of KEY->VALUE pairs such that each KEY is unique, describe a method of storing these pairs on disk, and a method for accessing the corresponding VALUE given a KEY. Assume that RAM is fixed at 1gb and the set of pairs requires 40gb.

HINT: we are trying to minimize page-transfers

Question #3) Given N computers networked together, with each computer storing N integers, describe a procedure for finding the median of all of the numbers. Assume that a computer can only hold O(N) integers (i.e. no computer can store all N^2 integers). Also assume that there exists a computer on the network without integers, that we can use to interface with the computers storing the integers.

Question #4) Given the sequence S1 = {a,b,c,d,...,x,y,z,aa,ab,ac.... } and given that this sequence corresponds (term for term) to the sequence S2 = {1,2,3,4,....}
Write code to convert an element of S1 to the corresponding element of S2. Write code to convert an element of S2 to the corresponding element of S1.

Question #5) Given a binary tree with the following constraints:
a) A node has either both a left and right child OR no children
b) The right child of a node is either a leaf or NULL

write code to invert this tree. HINT: Draw this out

Question #6) Given a square with side length = 1, describe all points inside square that are closer to the center of the square than to the edge of the square.

Question #7) How many 0's are at the end of N!

hint: look at the prime factorization of N!

Question #8) Given an array A[string], an array of strings where each string represents a word in a text document. Also given 3 search terms T1, T2, and T3 and 3 corresponding sorted sequences of integers S1, S2, and S3 where each integer in Si represents an index in A where search term Ti occured (i.e. S1, S2, and S3 contain the locations of the search terms in our array of words). Now find a minimal subarray of A that contains all of the search terms T1, T2, and T3. Extend this algorithm for an arbitrary number of search terms.

hint: think of the brute force algorithm first

Question #9) Design a data structure that supports push(), pop(), and min() all in O(1) time

Q: "You are shrunk to the height of a nickel and your mass is proportionally reduced so as to maintain your original density. You are then thrown into an empty glass blender. The blades will start moving in 60 seconds. What do you do?"

Q: "How would you find out if a machine's stack grows up or down in memory?"

Q: "Explain a database in three sentences to your eight-year-old nephew."

Q: "How many gas stations would you say there are in the United States?"
=========================================================
 

hypn0tik

Diamond Member
Jul 5, 2005
5,866
2
0
I can do 5 and 6 and pretty sure can do 4. The others look pretty difficult.

Edit: Actually, I can't do 4. I underestimated the problem.
 

sao123

Lifer
May 27, 2002
12,653
205
106
Originally posted by: hypn0tik
I can do 5 and 6 and pretty sure can do 4. The others look pretty difficult.

Edit: Actually, I can't do 4. I underestimated the problem.


4??

2 arrays.
search one.
get index of the value you are searching for.
return the value of the other array at the same index.
 

itachi

Senior member
Aug 17, 2004
390
0
0
Originally posted by: sao123
Originally posted by: hypn0tik
I can do 5 and 6 and pretty sure can do 4. The others look pretty difficult.

Edit: Actually, I can't do 4. I underestimated the problem.


4??

2 arrays.
search one.
get index of the value you are searching for.
return the value of the other array at the same index.
couple problems with that approach.. it assumes that the set would already be stored as an array.. if it's not, then you're storing data needlessly. and searching through the set would take too much time for large values.

since an element from each set directly correlates with a value from the other and no element from one is mapped to or by multiple elements, you can do a simple conversion.
S1->S2 : s1 is the element from S1..
s2 = 0
while s1 not 0
s2 += (s1 - a) % (aa)
s1 >>= z
end

i could do 1,2,5,6, and 7.. 3 maybe, but it probably wouldn't be the best solution.. 8 reminds me why i wouldn't wanna work for google.
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
Originally posted by: sao123
Originally posted by: hypn0tik
I can do 5 and 6 and pretty sure can do 4. The others look pretty difficult.

Edit: Actually, I can't do 4. I underestimated the problem.


4??

2 arrays.
search one.
get index of the value you are searching for.
return the value of the other array at the same index.

Do with constant storage.

Edit: And hence in constant/linear time.

Edit2: chatted with a friend...think we got everything except 2, but we have an idea for it.
 

emattt85

Junior Member
May 14, 2005
2
0
0
Do people want to see solutions? Or is this just for looking-at value? Either way, it's some pretty intense stuff.
 

erwos

Diamond Member
Apr 7, 2005
4,778
0
76
Originally posted by: emattt85
Do people want to see solutions? Or is this just for looking-at value? Either way, it's some pretty intense stuff.
My thoughts on the solutions - probably not all correct, or even mostly correct:
1. You just need to determine the offset for each input stream to know where to place it in the output stream.
2. 40 indexed binary search trees - figure out which one you need on the query, and then just run right down it. Seems like there should be a more elegant solution.
3. The cheat has to do with the fact that you know the total amount of numbers - N^2. Just keep querying the machines for their next lowest number until you've gotten N^2 / 2. Depending on the exact rules, you might have to do a sorted buffer of some sort ("everyone who's got something greater than my biggest value, hand it over - OK, that's 3 values, put those on top, push the bottom 3 values out, increment count by 3" and so on.)
4. Elegant solution already presented, although it makes the assumption of internal character representation. Brute forcing might work better at the end of the day. Make use of the fact that this is altered representation of base 26 numbers.
5. Basic gist is to iterate over every node. If the parent > child, swap the two. Easiest if you can start from the bottom and work up (doubly-linked tree?).
6. Middle of square is (.5, .5), dividing line is (.75, .75), right? Thus, if the quare root of the product of the coords of the point in question is > .75 or < .25, it's closer to the edges. (One of the resident aerospace Ph.Ds confirms this answer as being probably correct.)
7. Whichever is less: N!/2 or N!/5. Be thankful they only asked for it in base 10 - or is that a trick question?
8. Unless I misunderstand this, you just want to know the first and last indexes of each search index. The very smallest of the "first indexes" is the first index of your minimal subarray, and the very largest of the "last indexes" is the last index of your minimal subarray.

-Erwos
 

itachi

Senior member
Aug 17, 2004
390
0
0
Originally posted by: eLiuDo with constant storage.

Edit: And hence in constant/linear time.

Edit2: chatted with a friend...think we got everything except 2, but we have an idea for it.
constant storage is unreasonable for a set of an unknown size.. if the set ends at zzzzz, you'd need a bit over 11GB worth of space to hold a single sequence.

2 seems pretty easy, conceptually, when u think about it. given that the goal is to minimize page faults, storing each key/value one after another isn't an option. if they're stored sequentially, you'd probably get a page fault on every node you visit. what i was thinkin of was to use a chained hashtable at the beginning with keys that map to binary search trees.. and have the tree nodes map to an offset in the file. after applying a bin packing algorithm, the trees would take up a minimal set of 4k blocks (or whatever page size is in use).
only problem with that is that if u have the worst-case bst, and that tree doesn't change, you're stuck with a crappy tree.

anyways.. what'd u have in mind, if not that?

edit: damn u erwos.. i was too slow.
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
constant storage as in O(1)...in this case, you store nothing.

I'm saying that the array method is inefficient.

Edit: maybe my terminology is bad...not a cs major/no algorithms experience, lol
 

erwos

Diamond Member
Apr 7, 2005
4,778
0
76
Originally posted by: eLiu
Edit: maybe my terminology is bad...not a cs major/no algorithms experience, lol
If we assume the sequences are infinite, as was implied by the OP, your solution cannot work. No computer has infinite RAM.

-Erwos
 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
Originally posted by: erwos
Originally posted by: eLiu
Edit: maybe my terminology is bad...not a cs major/no algorithms experience, lol
If we assume the sequences are infinite, as was implied by the OP, your solution cannot work. No computer has infinite RAM.

-Erwos

You can do it with constant storage, but then it could take an arbitrary amount of time (amortized O(N/2), where N is the number of elements in the list).

A trivial algorithm would be to search one list until you find the specified item, counting the number of items before that one. Then count that number of items into the other list, and you have the matching element. However, if the lists are potentially infinite, the runtime is unbounded.
 

erwos

Diamond Member
Apr 7, 2005
4,778
0
76
Originally posted by: Matthias99
You can do it with constant storage, but then it could take an arbitrary amount of time (amortized O(N/2), where N is the number of elements in the list).

A trivial algorithm would be to search one list until you find the specified item, counting the number of items before that one. Then count that number of items into the other list, and you have the matching element. However, if the lists are potentially infinite, the runtime is unbounded.
I'm almost 100% sure the OP meant this as a mapping problem, not an array search problem. The usage of the sequence terminology versus array terminology implies it, anyways. There's also no explicit mention of having access to these arrays - it's a mathematical description of the map. Besides, the mapping conversion solution is so easy that it's kind of silly to bother with anything else.

-Erwos
 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
Originally posted by: erwos
Originally posted by: Matthias99
You can do it with constant storage, but then it could take an arbitrary amount of time (amortized O(N/2), where N is the number of elements in the list).

A trivial algorithm would be to search one list until you find the specified item, counting the number of items before that one. Then count that number of items into the other list, and you have the matching element. However, if the lists are potentially infinite, the runtime is unbounded.
I'm almost 100% sure the OP meant this as a mapping problem, not an array search problem. The usage of the sequence terminology versus array terminology implies it, anyways. There's also no explicit mention of having access to these arrays - it's a mathematical description of the map. Besides, the mapping conversion solution is so easy that it's kind of silly to bother with anything else.

-Erwos

Ah. The problem's not really clear. But yes, if you are allowed to use the knowledge of what the elements are, then it is just a trivial language mapping problem.
 

hypn0tik

Diamond Member
Jul 5, 2005
5,866
2
0
For number 4, I was under the impression that you had to come up with an algoritm to convert aa to a number and vice versa.

With that assumption, you can map each letter of the alphabet to a number (1-26), where a = 1, b = 2, ...z = 26

For example, 'hello' would be represented numerically as 8*26^4 + 5^26^3 + 12*26^2 + 12^26 + 15*26^0. Coding this would be fairly simple.

To get from number to string, you do the exact opposite. Divide by the highest power of 26. Map the quotient to a letter and store it in a string. Divide the remainder by the next highest power of 26 and keep going till you end up with a number less than 26. Once you obtain the next quotient, concatenate (sp?) the corresponding letter to the end of the string.

Does this look right to anyone else?
 

interchange

Diamond Member
Oct 10, 1999
8,026
2,879
136
int number_from_string(char *string) {
int val;
for (val = 0; *string; string++)
val = (val * 26) + (int)(*string - 'a')
return val;
}

void string_from_number(int num, char *string) {
int i;
for (i = num/26; i >= 0; i--) {
*(string + i) = 'a' + (char)(num % 26);
num /= 26;
}
}
 

DrPizza

Administrator Elite Member Goat Whisperer
Mar 5, 2001
49,601
167
111
www.slatebrookfarm.com
Wait a second... didn't I just see someone posting question #1 the other day?
Sorry, ones3k, but I haven't seen many of your posts around here.
When question 1 was originally posted, I and several others suspected it was homework help.
Now, it's being posted again (along with other questions)... is this an elaborate way to get help with homework, or were these honestly google questions?

Hmmmmm.....
 

DrPizza

Administrator Elite Member Goat Whisperer
Mar 5, 2001
49,601
167
111
www.slatebrookfarm.com
Yes, here's the thread:
here

Same OP, question 1 is still the same. It's funny that the context in which the question was asked the first time (and resulted in minimal help) didn't mention Google. Now, over a week later, this was a Google interview question?

HMMMmmmmmmmmmm
 

hypn0tik

Diamond Member
Jul 5, 2005
5,866
2
0
Originally posted by: DrPizza
Wait a second... didn't I just see someone posting question #1 the other day?
Sorry, ones3k, but I haven't seen many of your posts around here.
When question 1 was originally posted, I and several others suspected it was homework help.
Now, it's being posted again (along with other questions)... is this an elaborate way to get help with homework, or were these honestly google questions?

Hmmmmm.....

Does it really matter? If you want to help someone with their homework then do so willingly. If not then don't. If someone asks for help and doesn't show an attempt at the problem, you don't need to help them.

Regardless of whether these are homework questions or not, they are definitely interesting problems worth taking the time to solve (at least in my opinion).

Sorry for digressing off topic.
 

TuxDave

Lifer
Oct 8, 2002
10,571
3
71
Originally posted by: DrPizza
Yes, here's the thread:
here

Same OP, question 1 is still the same. It's funny that the context in which the question was asked the first time (and resulted in minimal help) didn't mention Google. Now, over a week later, this was a Google interview question?

HMMMmmmmmmmmmm

Good catch. I did recognize the first question with the streams but I never put 1 and 1 together. The more interesting part is that somehow by calling them 'Google' questions people think that the questions are more interesting. If this is what Google asks during an interview, I think I find the Microsoft ones more interesting.
 

DrPizza

Administrator Elite Member Goat Whisperer
Mar 5, 2001
49,601
167
111
www.slatebrookfarm.com
btw, I glanced at the solutions posted in the off topic thread...

a parabola is the set of points equidistant from a fixed point (locus) and a directrix (line)

So, using the center as the focus and each edge as a directrix, you wind up with the shape that is the intersection of 4 parabolas.
If you placed it anywhere on the coordinate plane, it'd be a piece of cake to write actual equations.

ones2k, is the bolded part a complete enough description? Or do you want actual equations, which would be a piece of cake, especially if you centered the square on the coordinate plane with center (0,0)
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
Originally posted by: erwos
Originally posted by: eLiu
Edit: maybe my terminology is bad...not a cs major/no algorithms experience, lol
If we assume the sequences are infinite, as was implied by the OP, your solution cannot work. No computer has infinite RAM.

-Erwos

I'm trying to say that you don't have to store anything. It's essentially conversion from base 26 to base 10 & back again. Though apparently I failed at making that clear. Like I said...I'm hardly the expert on CS terms...sorry about that.

That's good use of the definition of a parabola DrPizza. I hadn't originally thought of it in terms of the definition... If you divide the square into 4 quadrants by the diagonals, then say all the points in the right-side triangular quadrant are closest to the right wall. Hence the distance is only a function of the x-coordinate...and you have the distance formula to relate back to the center of the square. Set them equal, get a parabola.

Still your observation makes the simple math even simpler, haha.