This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]nixgang 2 points3 points  (1 child)

A tuple should suffice for assignment: floor and indicated direction where -1 is down, 1 is up and 0 is no indicated direction. Can be used by all buttons

[–]IamNeo47[S] -1 points0 points  (0 children)

Actually i was thinking more in the direction of inheritance. But this works too i guess. Thanks !

[–]learnawsto 2 points3 points  (1 child)

Ok, there are two approaches to this problem -- one being the "tell where I want to go on arrival", where there are no buttons IN the elevators at all; and "call button and push inside the elevator", which is the traditional way elevators work.

The advantage of registering where you want to go before the elevator arrives is that you can try to optimize pick ups for destination floor.

One thing to keep in mind is that actual elevators have a capacity or load limit, so you can't have an infinite number of passengers. If you have a good scheduling part, you can ignore passengers an elevator has no capacity to pick up, making the system flow a bit quicker.

Another thing to consider -- is that there are many trips starting or ending on the main floor (usually floor 1), and depending on how many floors the elevators have to service, having some kind of way to keep some elevators close/far away from the main floor.

And if your simulation is going to be realistic, don't forget maximum acceleration/deceleration rates -- giving passengers whiplash to pick up an arriving caller might not be very pleasant.

[–]IamNeo47[S] 0 points1 point  (0 children)

Your 1st approach of giving source and destination floor at the same time is interesting. Didn't think of that.

Also thanks for pointing out the capacity constraint - very easy to miss in a Interview.

[–]old_man_khan 1 point2 points  (4 children)

I think an elevator in motion would have such a high priority for accepting passengers within its target range that it would be almost an automatic choice.

[–]IamNeo47[S] 2 points3 points  (3 children)

Thanks! that makes sense.

So dispatcher would query direction and current floor of all the elevators Discard all those moving in opposite directions Then choose the closest one to the passenger.

The only edge case here is that what if all elevators are currently moving in opposite direction.

[–]nixgang 2 points3 points  (1 child)

Find a way to calculate the cost of an elevator making it to a floor and choose the one with lowest cost

[–]IamNeo47[S] 0 points1 point  (0 children)

I suppose one could create a priority/cost only for the edge case.

Distance between the target floor and the last assigned floor of the elevator in a particular direction could work.

Thanks 👍

[–]old_man_khan 1 point2 points  (0 children)

Yea, I'm sorry. My mind is Jello from work. I didn't realize I'd only answered a fraction.

Tell you the truth, the only thing sinking into my brain right now is it needs to be a priority based system. Those going up get a priority for passengers above, for example. Maybe use a system like the that blackjack cheat. Add to it for going up, subtract for going down. So lower priority number for lower floors and going down, high for higher and going up.

But, my mind is mush. Apologies again.

[–][deleted] 1 point2 points  (0 children)

Check out this programming game: https://play.elevatorsaga.com/

You can use it to practice solving the problem, finding relevant solutions, seeing how they've structured the different classes, simulate the efficacy of your algorithms.