Amazon Interview Question for Software Engineer / Developers






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

hash is not a good choice specifically for "phone" apps. You would want the items to be displayed sorted for which you need extra space when you have information in a hash table. A tree is certainly a better option

- PL April 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

First , the structure of a node in a trie
struct Node {
char c;
struct Node *next[26];
};
Have a constructor which initialises next[i] = NULL, for 0<=i<26

Node* Insert(Node *root, string s) {
Node *p = root; //save root pointer
for(i=0,n=s.size();i<n;i++) {
if(root->c!=s[i]) break;
else {
root = root->next[s[i]-'a'];
}
}
if(i<n) {
while(i<n) {
Node *temp = (Node*) malloc(sizeof(Node));
temp->c = s[i];
root->next[s[i]-'a'] = temp;
root = temp;
}
return root=p;
}

- Sriram April 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can anybody answer this question please

- Anonymous March 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use hash table to implement the phone book application. Use the hash function on the names and store the phone numbers in the table.

- Anonymous March 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

He asked to use a trie. I guess he may want to see how you deal with the nodes. Once I failed because I simply used "node_t *pnode[26];" to store the path.

- M March 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in for loop, in else case , I guess it should be
if ( i < n-1 ){
root->root->next[s[i+1]-'a'];
}
in while loop , i should be incremented.
let me know, if i am not correct.

- bansal.cool June 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in for loop, in else case , I guess it should be
if ( i < n-1 ){
root= root->next[s[i+1]-'a'];
}
in while loop , i should be incremented.
let me know, if i am not correct.

- bansal.cool June 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in for loop, in else case , I guess it should be
if ( i < n-1 ){
root= root->next[s[i+1]-'a'];
}
in while loop , i should be incremented.
let me know, if i am not correct.

- bansal.cool June 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would say a linked list(with all names in sorted order) and a trie (of all names).

Lookup is a frequent operation in a phone book while insert is not so frequent. This explains the choice of the data structures.

Also, the trie will help in providing the user with suggestions when he starts searching by typing the first few characters of a name.

- Mahesh October 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hash is only good for static phonebook.
Use balanced search tree if you want to implement dynamic phonebook. AVL tree or red-black tree is a good option. They provide O(logn) gurantees on insert and delete

- Vinay March 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think trie ( in compressed form) is the best choice for this type of questions. Coz as we all agree that phone-book's main operation is search : which can be done in O(m) time in a trie and easy updation , more importantly this time complexity is independent of the nodes and strings in them like in balanced trees which takes O(mlog n) time.

- mrn July 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Have a pre-processor which converts Names to Numbers. Then traversing a tree will be much easier.

- PK July 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

-Trie for sorted access of names and
-HashTable of <name,number> to retrieve number from name

- krutik January 29, 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