Microsoft Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Free list?

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

A free list is a data structure used in a scheme for dynamic memory allocation. It operates by connecting unallocated regions of memory together in a linked list, using the first word of each unallocated region as a pointer to the next. It's most suitable for allocating from a memory pool, where all objects have the same size.
Free lists make the allocation and deallocation operations very simple. To free a region, one would just link it to the free list. To allocate a region, one would simply remove a single region from the end of the free list and use it. If the regions are variable-sized, one may have to search for a region of large enough size, which can be expensive.

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

can you please explain more about the data structure and how it work in this scenario? I tried searching over this data structure over the internet but could not get much good info..

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

At this point, I'm not sure how much memory we have considering we have a million parking spaces.
One possible solution is to use a bit vector. Each spot would be
0: spot available
1: spot occupied.

Thoughts?

- smffap March 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Use a priority Queue.
Assuming each parking slot will be having a unique slot number associated with it.and lets also assume that parking spaces on the lower floors get more priority as compared to parking slots on higher floors.
When a new car comes in looking for a parking space just pick pop the top most entry from the Priroty Queue to get the best parking space.

- BJ September 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If we use a priority queue here, then how will we be taking the vehicle out of the parking lot?

We need random access to each vehicle. So a map/hash-map is I think the way to go.

Also, the parking space is going to be limited; and we have to think about parking a 2wheeler maybe in an empty slot of a 4 wheeler and vice versa.

- Akheel October 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Akheel,
priority q would contain all the AVAILABLE parking lots.
when you take a vehicle out of the parking lot, which means you would free up a parking space, which inturn leads to insertion of the parking space object into the priority queue.

But since there are going to be millions of parking spaces performance could be an issue with this approach.

- BJ February 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

I believe a bit array data structure will be appropriate. The parking lot can be modeled as a bit array with all 0s to begin with. When the cars begin to fill in turn the 0s at appropriate positions to 1s. Later on, just go through to the array to find the 0s which are still empty spots.

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

I think in worst case time complexity will be O(n) for finding a free spot.. Correct me if i am wrong..

- beginner August 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

as per my understanding, what is the reason we need to consider only till a group of 10.
say we have an array essentially a integer array in which each array member refers to 100 locations each. The array can take values from 1 to 10 0 saying that there is no place avaible. a value of 1 means that a slot is available in the first 10 slots of the level.

to take it more generic. 100 will be the number of cars a level of car parking can host. and 0 will be the a not available state, and starting from 1 we can basically say whether the slot is available or not, if it is not zero then where it is available.

Thoughts ?

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

I this is a viable answer. As there are millions of parking space. On the other hand you can check free space by bit operation as they are extremely fast.

- Riyad Parvez January 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

To maintain allotted parking slots, I think dynamic hash array would be better since finding and maintaining is fast.
One vector or dynamically resizeable queue is for maintaining free list of parking slots. The first slot from vector will be allocated for new parking request and details will be stored in hash.

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

Can you please explain with an example? So for e.g. I had initially 100 ParkingSpot objects, how they will be maintained as the parking gets allocated and deallocated.

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

Use Hash<spaceid, Queue<FreeSpaces>>
Hash<spaceid, count of space available in individual queue>

we cannot have all the spaces in memory. So we can cluster the spaces together using some method. Example - if their are 100 spaces, I can use %10 to create 10 spaceids.
now each spaceid will now have 10 spaces. for them we create a queue<free spaces>
whenever a space is required for parking, it is dequeued from queue.

So lets say I want 10 free parking spaces - I will just iterate over the hash<spaceid, count> and get the correspoind spaceids. Then search in Hash<spaceid, queue> and get those free spaces.

Let me know what you think?

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

It looks good, Ravi. With SpaceID you mean the parkingspot no? Can you write little code to demonstrate how the findfreeparkingspot method will return the parkingSpot no and UnPark will make the parking spot no. again available for parking.

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

its min heap , or a simple tree , one node is parked and other is un parked , you maintain both of them ad get parking spot in just O(1) time

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

Use Hash<spaceid, Queue<FreeSpaces>>
Hash<spaceid, count of space available in individual queue>

we cannot have all the spaces in memory. So we can cluster the spaces together using some method. Example - if their are 100 spaces, I can use %10 to create 10 spaceids.
now each spaceid will now have 10 spaces. for them we create a queue<free spaces>
whenever a space is required for parking, it is dequeued from queue.

So lets say I want 10 free parking spaces - I will just iterate over the hash<spaceid, count> and get the correspoind spaceids. Then search in Hash<spaceid, queue> and get those free spaces.

Let me know what you think?

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

Use Hash<spaceid, Queue<FreeSpaces>>
Hash<spaceid, count of space available in individual queue>

we cannot have all the spaces in memory. So we can cluster the spaces together using some method. Example - if their are 100 spaces, I can use %10 to create 10 spaceids.
now each spaceid will now have 10 spaces. for them we create a queue<free spaces>
whenever a space is required for parking, it is dequeued from queue.

So lets say I want 10 free parking spaces - I will just iterate over the hash<spaceid, count> and get the correspoind spaceids. Then search in Hash<spaceid, queue> and get those free spaces.

Let me know what you think?

- ravi February 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Maintain a MinHeap(Priority Queue) of free Parking spots. Whenever a freespot is occupied/added ,Reheapify it..It takes O(log N ) time only..

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

H: Heapify
a: array of parking spaces
CE: Car enters parking lot
CL: car exits/leaves a parking lot
l: left
r: right

H(i, mx)
	l = 2*i+1
	r = 2*i+2
	max = i
	if(a[max] < a[l] && l <= mx)
		max = l
	if(a[max] < a[r] && r <= mx)
		max = r
	if(max != i)
		swap(i, max)
		H(i, mx)

a[n]  = {0}

CE()
	a[0] = 1
	H(0,n-1)

CL(i)
	a[i] = 0
	H(i, n-1)

- Prateek Caire February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

So the data structure we can take a bit vector and then keep heapify it.

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

MAP[ParkingNumber] = Occupied/Not occupied

- Guru June 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can a BST be used to store all free spaces..
when allocation or deallocation is required,u can allocate a particular sized location by traversing the tree..
any comments

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

make a hash table of size=num_of_rows
then store the free column no. in a particular row according to the hash function..
size wont be that big

- lilprogrammer July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

make a hash table of size=num_of_rows
then store the free column no. in a particular row according to the hash function..
size wont be that big

- lilprogrammer July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

anybody know full coding about car parking by using data structre please email to me..haziela93@yahoo.com..very urgent...please

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

Use a priority Queue.
Assuming each parking slot will be having a unique slot number associated with it.and lets also assume that parking spaces on the lower floors get more priority as compared to parking slots on higher floors.
When a new car comes in looking for a parking space just pick pop the top most entry from the Priroty Queue to get the best parking space.

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

What about QuadTree using a map as internal storage element??

private class QuadNode < Value extends Object>
{

QuadNode [] quad ;


Map < String, Value> map;
QuadNode (String key, Value value)
{

if(key== null || value == null)
throw new NullPointerException();

if(quad== null)
quad = new QuadNode[4];

if(map== null)
{
map = new HashMap<>();
map.put(key, value);
}
}



The token for the car can be generated using base4?
So if you have 1 million points, you can first put a few in the map, say if the threshold is 500.
and then split it in 4 quadrants. Time complexity O(n)
Also, you can generate the token id using base 4 , which maps to quadnodes[i] inside the data structure?

Any opinions on that??

- jimmy514in March 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Maintain two vectors. One for free slots and one for occupied slots. So whenever we search for free space the free slots vector contains all of the slots that are unoccupied. When a vehicle wants to occupy a free slot , remove that parking slot and insert into the occupied slots vector. Similarly if a Person wants to leave the parking area remove that parking slot from occupied Slots vector and insert it into free slot vector.

- Vaibhav Raj August 11, 2022 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Use Page tables

- Abhi February 24, 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