Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Use two data structures
1. An array of size N, with index nextEmpty, which indicates where the next element should be added.
2. A hashmap with key as the number itself and value as the index of that number in the array.
For add operation - Just add the number at nextEmpty location, update hashmap and do nextEmpty++.
For remove operation - Get the index of the number from hashmap, remove it from hashmap. Then swap last element in the array(nextEmpty-1) with that element. Update the hashmap and do nextEmpty--
Both operations will be O(1)

- deadman September 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 5 vote

<Programming Pearls> has the answer:

two array: from[], to[] (from[to[i]] == i).
initial:
top = 0;
push one element:
to[top] = data; from[data]=top++;
check data:
to[from[data]] == data

- Anonymous September 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

If we can do this, this i will give you one advice store data into from inself ,not in top, and enqueue operation is from[data] = data;
and similarly dequeue operation can be written, and this require less amount of memory, so what is this,

- sonesh November 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

i don't know what sonesh is saying.

I have another doubt.
Take the case:
While searching, data = 5
5 has not been inserted in the hashmap

Garbage values are as follows:
from[5] = 3234
to[3234] = 5
Then this will return that 5 exists but if we have the check from[data]<top, then it will function correctly

So I think the check
from[data]<top
should also be there.

- Tushar January 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In your solution, how are you implementing the constraint of HashSet size being 'M'?

- Learner August 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

by a size variable...
if no elements are added then size=0.
gradually on adding elements size will increase and finally it will become M.
When size will be M then no more elements can be added.

obviously things will be reverse for deleting elements from hashset.

- aditya.eagle059 August 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 4 vote

You could use an array of linked lists of size M. The hashing function could be (val % M). Every time you have to insert a no., you pass it through the hashing function to get the index where the no. would be inserted.
If there isn't any linked list at that index, you will have to create a node. And then that index would have to point to that node. Thus in this you don't need any initialization in the beginning.

- Jack August 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

allocate linked list of size M is the same as array of size M. When you allocate any node in linked list, you need to initialize the node. I am feeling that is same.

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

1. Create an array of size M.
2. Since N>M, collisions may occur. Use a linked List for collision(most common).
3. Find element using key(or value, if key is the value) and hashcode.

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

that way takes O(n) space and n is very large. implement a hash function for size m if collision occurs maintain a list

- t.deepak.iitg September 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

maybe BST, each node in BST is a linked list?
However, this solution will increase the complexity of the algo

- Vincent September 17, 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