Amazon Interview Question for SDE-2s


Country: India
Interview Type: In-Person




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

We can use Trie data structure

- Abi August 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Trie will be not efficient when you are trying to perform search using phone nos

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

problem wont describe it needs search on phone numbers as well.

By the trie would be solution when each word in contact stored separately OR use sorted array of words in each contacts.

If you need to search on phone number as well then nothing worry, use numbers in same data structure you are using. In this case, you will have all words of all contact + numbers as index.

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

with trie the following result will not be obtained
typing 'ama' wll give

Aman
Amazon
Neha Aman

- anurag6989 August 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Yes trie is the data structure for Name (First name, and last name). and It will required one HashTable for number for fast searching.

1. Trie will contain two node - one for First name, and one for Last Name. To provide search based on both - first and last name.
2. Each trie node will contain phone number to fetch contact details(Name, address etc) from HashTable

I am considering only 26 Captal letters and 26 small letters for name, so number of keys in a trie will be 52.
Time Complexity - O(logL base 52), where L = length key (first name or last name)

Disadvantage:
Trie is unbeatable in terms of time, but it is space monster. So we can use Ternary Search Tree (Each node can have maximun 3 children) in place of Trie

Time Complexity - O(logL base 3), where L = length key (first name or last name)

If you want to provide prefix based search capability in number too, than use one more trie for number in place of HashMap.

- Ajeet August 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

how about a trie, in which the leaf node pointed to a hash_index in a hash map of phone numbers.
This way you can search names efficiently by using the trie. And also, be able to search for phone numbers easily by directly using the hash map.

- sagar August 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry this is not a good solution because it doesn't allow prefix search by phone number (entering 123 won't list 1234 and 1235).

- gameboy1024 August 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

can someone tell me the answer for the problem

- vishnu August 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution should be extendable.

I would use Trie that stores everything, not only names.

The contact can be stored in a normal object. Some properties should be marked as "indexed" like name, phone number, company, and others "unindexed".
The Trie should then store more than just the name:
- First name
- Last name
- First + last
- Last + first
- Phone number
- Company
- Location
etc.

Leaves are pointers to the contact object. The Trie should be updated when contacts are added/modified/deleted.

- gameboy1024 August 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Store each word and number in compressed tries or ternary search. No matter It is first name or last name. It is just word. At the lead node (ideally word) have reference to HashMap <Key, ContactDetails> ... By doing this, It is possible to fetch all the details of personal. Phone number, Name, email etc.. As List is already in memory so, there will not be any additional space for pointing refereces. T(n) - word length.

- Anonymous August 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

to find matches based on prefix, we can use a binary tree. It would be self-balancing, for example a red-black tree, so all operations inseer/modifiy/delete/find will be O(n).

- gen-y-s August 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't know about your binary trees, but mine do all operations in log(n)

- Anonymous August 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

array of structures (y)
pattern matching for strings..

- Tanuj August 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use 1 trie per search vector (first name, last name, phone number). Each trie will have some height (not to exceed the longest name/phone number).

Attach the data leaves (the actual data we are looking for) to the phone number trie (most looks ups will probably come during in-coming calls). Make the leaves for the first name trie the roots of the last name trie. Make the leaves of the last name trie the roots of the phone number trie. This results in a large trie which is logically "first name"->"last name"->"phone number" of height h.

During look up, for the first character you will need to perform h look-ups into each level the final trie, since we could be searching in the middle of a name, and maintain a list of some sort (which to get the set of names for a given letter will result in O(h) look-ups assuming you want real-time update of the possible list while typing). For each additional letter, you will prune that list.

Some optimizations are to maintain points/references from each level of the trie to all leaves below it. This will allow for faster pruning and data look-ups since you don't have to traverse the entire tree to get the to the leaf nodes. A similar tactic can be used with parent pointers in leaves too all nodes in the path to the leaf to make for quicker deletion

- Anonymous August 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just use one suffix tree for all keys (first name, last name, phone number). Reference the Contact data structure from the leave of the tree.

- Teh Kok How September 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can we store all the suffixes of the strings in a trie ? In this way we can search efficiently .

- anonymous September 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes this idea look better

- rahul February 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Below data structure would require only o(1) time to search/add/delete. We are building hash table of prefix and suffix of string and are going to store index of actual string along with value below is sample structure

[0]Aman
[1]Amazon
[2]Neha Aman

--
1 ] Aman[0]
aman
man
an
n
a
am
ama

2] Amazon[1]
amazon
mazon
azon
zon
on
n
a
am
ama
amaz
amazo

3] Neha Aman[2]
neha
eha
ha
a
neh
ne
n

aman
man
an
n
a
am
ama


HashTable===>
[key01]-->[aman]
	   --> 0
[key02]-->[man]
	   --> 0
[key03]-->[an]
	   --> 0->2
[key04]-->[n]
	   --> 0->1->2
[key05]-->[a]
	   --> 0->1->2
[key06]-->[am]
	   --> 0->1->2
[key07]-->[ama]
	   --> 0->1->2

[key11]-->[amazon]
	   --> 1
[key12]-->[mazon]
	   --> 1
[key13]-->[zon]
	   --> 1
[key14]-->[on]
	   --> 1
[key15]-->[amaz]
	   --> 1
[key16]-->[amazo]
	   --> 1

[key21]-->[neha]
	   --> 2
[key22]-->[eha]
	   --> 2
[key23]-->[ha]
	   --> 2
[key24]-->[neh]
	   --> 2
[key25]-->[ne]
	   --> 2
[key26]-->[aman]
	   --> 2
[key27]-->[man]
	   --> 2

- ramajumale February 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

partial string match, I would choose trie

insert: add first name, last name to construct the trie
search: whenever user inputs a letter, traverse the trie and return the subtree; prone the tree when get one input; show the results from the tree using dfs
delete: find the path, remove the label
modify: delete & insert

- Sean Locke April 21, 2016 | 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