Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

Bit Vector
hash Map

Bit vector can save space as we already know the range. 1 means the number is there and 0 means it is not there. inserting a number ans checking a num is O(1).
also searching is O(1)

- King@Work February 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How about 4th utility get_any_number ?

- abzx12 February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If any means random here then we can generate random number by using some algorithm/function. This will give the number or index.

If it has some different meaning meaning then let me know.

- King@Work February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you surely can check random no. but we can't be sure that its present there in DS, and if not then time is no more constant.

- abzx12 February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If we can use additional memory this is possible. Did the interviewer gave you any hint??
The worst case of the proposed design can fall back to O(n)

- King@Work February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No hint :( but no constraints on memory.

- abzx12 February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Then we can maintain an array of the occupied indexes. Whenever we insert an number we add tht to the array. and the random number picks the index from the array.

- King@Work February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

but then deletion takes more than constant time.

- abzx12 February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

BitVector wont work as we need to delete number and if there are two identical numbers added and one deleted.
So we need to use int array.

- loveCoding February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

right. wat abt hash map? :) it solves the problem. Good thing about hashmap is that it is not ordered. Though I doubt that everytime you get a number it is same or not. But the hashmap design says that there is no gurantee that the order is same every time iterate.

I think there should be some better solution of this.

- King@Work February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice point. Are there duplicates allowed?? I was thking there are no duplicates.

- King@Work February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

So are we sure get_any_number means get random number?

- eugene.yarovoi February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Anyway, if numbers are 1 to 1000, the approach of keeping a size, choosing a number N at random from 1 to size, inclusive, and finding the N-th smallest element in the array (just by seeing how many elements are in the 0-th bucket, then 1st bucket, etc.) is technically O(1) because the number of buckets is fixed.

- eugene.yarovoi February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use int array of 1000, where index0-represents 1 and index-999 represnts 1000.
Add just increase the array value by 1
Delete just decrease the array value by 1
Search just check if array[number-1] >0
Get any number is similar to search

- loveCoding February 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Forgot to mention with ques that numbers are unique. Moreover your idea is same as Upt's. We can't guarantee constant time for get_any_number.

- abzx12 February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you tell me what get_any_number exactly is?
More specifically does it delete the number from DS

- loveCoding February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Forgot to mention with ques that numbers are unique in DS, means max of single occurrence of a number.

- abzx12 February 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Forgot to mention with ques that numbers are unique in DS, means max of single occurrence of a number.

- abzx12 February 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Forgot to mention with ques that numbers are unique in DS, means max of single occurrence of a number.

- abzx12 February 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create a prefix tree for every number that is added by using each digit as a node.
1. To insert, just move along the tree if the digits exist or create and move on. Do this 4 times for each number O(1)
2. Delete: Move down the tree 4 times using the digits and kill the root node (1)
3. Search: Move down the tree 4 times. If node missing, return -1 O(1)
4. Get any: Move down the tree 4 times choosing child nodes at random. Report the number O(1)

- axecapone February 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hash table over a doubly linked list.
1. Insert - Head of linked list and in hash table. If already present just increase the count (can be stored in the linked list element).
2. Delete - Find node using hash table, decrease count. If count is zero, delete node (its doubly linked list so O(1))
3. Search - Hash table O(1)
4. Get any number - Return head of linked list or -1 if head is NULL

- Anonymous February 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hash table over a doubly linked list.
1. Insert - Head of linked list and in hash table. If already present just increase the count (can be stored in the linked list element).
2. Delete - Find node using hash table, decrease count. If count is zero, delete node (its doubly linked list so O(1))
3. Search - Hash table O(1)
4. Get any number - Return head of linked list or -1 if head is NULL

- Anonymous February 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Q2. Given the following linked list, answer the questions:
L
‘B’ ‘E’ ‘S’ ‘T’
a) Determine the output of the following: (1 Pt each)
Note: Each part continue from the previous part.
node *P1=L,*P2=p1;
P1=(node*)malloc(sizeof(node));
P1->data=’E’;
1. P1->link=P2->link; P1->data= __E___
2. P2=P1->link->link; P2->data= __S___
3. P1=P2->link; P1->data= __T___
4. P2=P1->link; P2->data= __B___
b) Write one line of code to replace content of first node with
content of second node (in any linked list). (2 Pt)
L->data = L->link->data;
c) Write the code to delete the first node in the list. (2 Pts)
( assume last node points to null )
node *P = L;
L = L->link;
Free(P);

- Anonymous February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a doubly linked list and array.When a element i is to be stored create a node with value
i and add it to the linked list and store this node's address in the array at index i.when a
element is to be deleted find the node's address from the array and then delete it from
linked list and put null at the corresponding index in the array.When anyElement is called
return the first element of the linked list.

- bhokal February 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think a bit vector will be the most optimum solution since we can have at the most one occurence of a number. We will need as much as 32 integers to store 1024 bits.

Insert : set_bit
Delete : reset_bit
Search : get_bit

class Bitset{
      private:
              int *bitset;
      public:
              Bitset() : bitset(NULL){};
              Bitset(int size): bitset(NULL)
              {
                       bitset=new int[size>>5];
              }

              bool search(int pos)
              {
                     int word=pos>>5;
                     int bitnum= pos&0x1F;
                     return ( bitset[word] & 1<<bitnum )!=0 ;
              }
               
              void insert(int pos)
              {
                     int word=pos>>5;
                     int bitnum=pos & 0x1F;
                     bitset[word] |= 1<<bitnum;
              } 
              
              void delete(int pos)
              {
                     int word=pos>>5;
                     int bitnum=pos & 0x1F;
                     if( !search (pos) ) bitset[word] |= 1<<bitnum;
              }

- maddy March 14, 2012 | 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