Google Interview Question for Software Engineer / Developers


Team: YouTube Engineering
Country: United States
Interview Type: Phone Interview




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

If all they want is a data structure to efficiently support add, delete, and pick one at random, i.e. a toy case and not asking you to design a full load balancing server system, you could do it with a simple array. Allocate an array big enough for what you think you might need. Have an integer variable you use as a pointer to the next unused element. To add an item you put it in the next free element of the array and increment the pointer. To delete an item, copy the last item over it and decrement the pointer. To select one at random generate a random integer between 0 and the index of the last item. All of that is O(1). If you run out of space when adding, a typical implementation is to allocate a new array twice the size of the current one, copy the elements over and free the memory of the original array.

- pollywog December 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

Seems like double linked list + hashmap is a viable solution.

Add "servers" as a double linked list, and keep track of where they are on the list by using hashmap. This will give you O(1) on add, delete, and pick random (given that you have a random number generator that works on O(1)).

- Anonymous November 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

assume that you inserted three servers:
a->b->c
hashtable:
a
b
c

then you deleted b
new hashtable
a
b null
c

the random generator gives you 2
you got null
now you need to execute the random generator again
the tricky part is how to deal with this situation in constant time

- hydrofuel November 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In no situation of add can be completed in O(1) using your data structure, since if it is given you the position to add, since you need to maintain the double-link-list, it can be O(n). If you are provided the previous node for you to add the new node, you need to know the positions to maintain(update) hash map, counting positions takes O(n) as well. Correct me if I am wrong

- TonyPras December 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

they asked to create your own data structure and write a method in any programming language of add, delete and pick at random.

- annonymous November 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

thats correct!! thats what they wanted

- annonymous December 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The hashmap answer is correct, if lacking in details.
The problem spec does not require that we pull the servers from a list, so I assume that the list representation is not required.
Adding it is more or less trivial.

We want an implementation of sets which support some form of random selection. My personal favorite language, if asked for implementation, in interviews is python because it provides this sort of utility for us. You could look up the details in the python svn repository if you want to translate to another language.
I recommend looking at set_next() and set_table_resize() if you go down that path.
You'll see that set_next does the naive walk over indices.
Essentially, by keeping the table appropriately sized, you avoid O(n) degradation in the average case.
Of course, there are pathological cases for this approach, but I'd consider that acceptable in an interview scenario.

Create two sets of servers. Call them

good

and

bad

Whenever you detect a server failure, remove it from good, and add it to bad. If a server comes back up, just remove it from bad and add it to good, naturally.
The trickiness comes in having a good implementation of sets, but many languages provide this for you. If not, python's C implementation is about as definitive as any.

If you're asked this in a phone interview, you'll almost definitely be allowed to assume the existence of a good hashtable implementation.
During the onsite, you had better know how to implement a good hashtable in any language. The easy strategy for resizing is to do it when the number of elements drops below some fraction of the table length.

- Stephen December 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please go into a little detail?

- lolokdontgetit November 17, 2012 | Flag


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