Interview Question


Country: United States




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

Maybe you could combine a hash table with a custom data structure that is backed by an array. The hash table would have an index into the array.

AddServer:
insert value at end of array: O(1)
insert value and array index in hash table: O(1)

RemoveServer:
get array index from hash table, delete entry from hash table: O(1)
replace array entry with last array value: O(1)
update array index in corresponding hash table entry: O(1)

ReturnRandomServer:
access array by index using random mod array size: O(1)

Not sure they would accept the amortized O(1) costs of the hash table though.

- Sir CodesALot April 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I like your idea....you always maintain the array in a compressed fashion so that a random operation is guaranteed to have a hit. I like it. Thanks.

- smallchallenges April 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This puppy is an interesting one. Thanks for posting. I do not have an answer...but I am hoping my thought process can generate a response with an answer :-)

O(1) add and remove is possible with a hash OR a double linked list. It is the return random server that is the problem.

I thought about probabilistic structures like skip lists and blook filter, could not get a guaranteed O(1). I thought about a bitmap of remaining servers...not O(1).

Would welcome a tip from somebody else :-) Thanks in advance.

- smallchallenges April 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Okay. Found a reasonable answer...Cuckoo hashing.
O(1) Lookup/Delete
Amortized O(1) Inserts.

Most credit to google and little credit to me...I was close when I was thinking bloom filter.

Thanks for the question once again!

- smallchallenges April 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

On further thought, I was hasty. The random server part is *NOT* solved by Cuckoo hashing. Sorry.

- smallchallenges April 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Perhaps if you restricted your search to careercup.

This has been asked multiple times before.

- Anonymous April 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You know...the search was not for the question...that would mean I am taking shortcuts...the search was for a data structure that can support the perf limits OR at the least generate an idea. What can you say? I am not smart that way :-)

- smallchallenges April 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi smallchallenges,

Can you explain how this addresses the RandomServer part? You note cuckoo hashing supports constant time lookup, delete and amotized inserts, but stated earlier that O(1) add and delete are already possible. So how does cuckoo hashing help with the Random server part? Thanks for clarifying :)

- johny418 April 09, 2014 | Flag Reply


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