Amazon Interview Question for SDE1s


Country: India
Interview Type: In-Person




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

Lets see what we have in hand and what we have to do.

we have to perform Insert/Delete/Search/Get Random all operations in O(1).
It is clear there is no data structure that satisfies all the requirements, some support one of the operations, some 2 but neither supports all 4 operations.

By now, it is evident we have to use Hybrid of 2 or more data structures.
Now question we have in front of us is which ones?

Lets move operation by operation and we will keep the ones that are present in maximum operations.

1. O(1) Insert
==========
Stacks/Queues/Linked lists and hash tables support this operation, at this step BST, heap,Skip list, TRIE etc are eliminated.

2. O(1) delete
===========
question doesn't clearly specify delete what? first element, last element or any element?
If we have to delete first element than we go for queues, if we have to delete last element we select stack, if we have to delete any element then we opt for hash.

so contenders in the list till now are - stack/Queues and Hash table.

3. O(1) search
===========
At this step both stacks and queues are ruled out and only hash table remains in the list.
so at this step we are clear that one of the data structure should be hash table.

4. O(1) Get Random
================
we have selected hash as our choice of data structure for this problem, but hash fails to fulfill this requirement, hash requires key to fetch any element and we have no way of generating random keys...now what to do?
Let's follow the approach we were following in our earlier steps:
which data structure satisfies O(1) random access?
There is only one Arrays.
Just give index + starting address and boom array gives you the result in O(1).

But our problem is still not solved, how to use this property with hash and also how can we generate a random hash key??


re generating random key, it is clear it is something we can't do because key might or might not be present in hash table but there is one thing we can do we can generate random integers.
How to use these random integers?

this is the final part of our solution simple keep all hash keys in an array, use a random number generator to generate a random number between 0 and totalNumberofkeys-1.

Fetch the key stored at this index O(1) and get value corresponding to this key O(1) again and return the result.


keeping array for storing keys involve inserting keys in array (insert op) and also deleting keys in array (delete op), I'll leave that to you guys for implementation.

- varun October 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

+1

But to cut things short...requirements 1, 2, 3 are the "usual" operations for sets/dictionaries/collections/whatever/datastructures.
So it's better if you take them together, and find the data structure(s) that satisfy 1/2/3....

Then try to satisfy 4 on top of that.

- S O U N D W A V E October 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

oh yes, absolutely.
stacks/queues/LL were eliminated in step-2 only, I was just trying to demonstrate how to approach a problem like this.

looking at requirement 1,2 and 3 Hashtable was an obvious choice .

And arrays offer random access so best choice for 4th requirement.

how to merge hash + array that was the basic question.

- varun October 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

When you delete a element then the key entry has to be deleted from the array index. Now this slot free and GetRandom() wont work in O(1) if it resolves to this slot in index. I think some more has to be extended here.

- rahul January 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Use requirements 1 to 3 -> What are some DSturctures that can do this?

Of the above, can you add operation 4 to any of them?

And just saying "O(1)" is a bit ambiguous..

- S O U N D W A V E October 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

We can use combination of doubly linked list and hashmap of <value,node>;
Each time we add a node in DLL , we will add it into hashmap also.
now lets see how it gives o(1) complexity.
1) insert is ofcource O(1). insert at frond or rear.
2) we can search in hashmap for the desired node.
3) look up in hashmap . if node present than get the node from hashmap. Since its a doubly link list , and we have pointer of node to be deleted , we can delete from DLL in O(1).
4) I am not sure what they expect in this one.

- priyank doshi October 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the answer can be found here: /question?id=6430339136225280

Use hash table + array. The hash table, on its own, should satisfy requirements 1 through 3. But in order to satisfy requirement 4, we use an additional array in addition the hash table.

- Alistair October 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This one is a "few" liner in Python, thanks to dictionary.keys() .

Anyone care to post code?

And we (meaning, comp sci community) are totally abusing "O" notation. Someone needs to reset its use.

- S O U N D W A V E October 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use linked hash map that would satisfy conditions mentioned above.

- Sutanu October 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use linked hash map that would satisfy conditions mentioned above.

- Sutanu October 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think it is looking for hashtable

- Anonymous October 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hashtable+arrayList

- Jason October 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hashtable+arrayList

- Jason October 15, 2013 | 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