Amazon Interview Question for Software Engineer / Developers






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

hash table

- Anonymous May 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what is the datatype of Hastable..and how each function will work?

- Nish May 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

you can store any datatype in hashtable...there are many ways to implement a hash table...simplest is array implementation..since the range of the valuse is given...we can safely implement hashtable using arrays...and we just have to store the count of each value.

other way to implement hashtable is to using self balancing binary trees but the insertion deletion lookup is O(logn)

- Anonymous May 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

tell me...by this how FindAny() will be O(1)

- Nish May 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

sounds like hashing to me

- pansophism May 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Surprised to see everyone who is claiming hashtable is right answer doesn't know that worst case time for insertion/search/delete takes O(n) time!

- anonymous May 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I didn't get what does FindAny do. But basically what u need is not exactly a hash table! You need an int[1000]. If findAny needs to return the last insertion the codes would be:

Integer lastInsert = null;
void insert(i){array[i]++; lastInsert = i;}
void delete(i){if(array[i] > 0) array[i]--;}
boolean isThere(i){return (array[i] != 0);}
Integer findAny{return lastInsert; /*it would be null if there is no insertion*/}

- Babak May 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if I deleted the value stored in lastInsert..how will you get findAny() ??

for ex: insert(10)->insert(8)->delete(8)->findAny()???

output should be 10..but your code will be still 8

- Nish May 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yep, people make bugs even in 4 lines and even on interviews.

- anonymous May 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@nish:
we can use the "random number generator" to find (findAny)number. findAny completely depend on randomness of "random number generator"

- anonymous123 May 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'm not sure why we even bother with Hashtable as the input will always be between 1-1000. We can simply store it in Array and ignore first index and map all elements 1-to-1.

I think if you just say hashtable you will fail since a "hashtable" is assumed to have collision so you should say a "perfect hashtable" or you should explain the fact since we know the range we can setup hashtable with no collision.

- Mat May 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use bitmap..
a[1000] -> initially it contains 0.. later it contains the number of occurences.
ex - If i have number 50 repeated 3 times then a[49] will have 3.

- swathi May 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

---CORRECT ANSWER---(everything else above is crap)
maintain following two DSs:
1. hash table (direct addressing table for 1-1000)
2. doubly linked list

for every insertion in hash table insert the element at head of DLL and store the pointer to this newly created node (DLL node) in the node in the hash table. during deletion from hash table, delete from DLL as well (deletion from DLL takes O(1) time)...going to the corresponding node is also O(1) as we've already stored the pointer for the node in DLL. for findAny return the head of the DLL. the isThere is trivial.
Further optimisation: club DLL into the hash table itself. needn't store the ptr to DLL node explicitly.


-PS

Courtesy: lallu

- ---CORRECT ANSWER--- July 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your solution is so bad.
Just an array of int[1000] which initialized to 0
insert will be O(1), since we just need to set 0 to 1 when we need to insert an element
Delete will be O(1), since we just have to set 1 back to 0
IsThere will be O(1), since there is no collision
FindAny, i dont know what they want......

- Mengjun December 01, 2011 | 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