elevator coding challenge
To build and run all tests use the following command:
sbt test
Elevator simulation interface is the following:
trait ElevatorControlSystem {
def status(): Seq[(ElevatorId, FloorNumber, Direction, Set[FloorNumber])]
def pickup(pickupFloor: FloorNumber, goalFloor: FloorNumber): ElevatorControlSystem
def step(): ElevatorControlSystem
}
-
To query the status - call
status()
method, it returns sequence of elevators states with the following data:elevatorId
,currFloor
andgoalFloors
for each elevator in the system -
To request a pickup - call
pickup()
method, it accepts two parameters:pickupFloor
andgoalFloor
, the direction is the following:pickupFloor
<goalFloor
- going up (+
)pickupFloor
>goalFloor
- going down (-
)
-
By calling
step()
method the system will simulatetime-stepping
:- elevator that is empty (no goal floors) - is unchanged (only if there is no new pickup requests)
- elevator that is between floors - going to the next floor
(
currFloor
+
/-
1) - for elevator that is on either
pickupFloor
orgoalFloor
the system will eitheradd
orremove
goal floors accordingly
-
Since both
pickup()
andstep()
methods alter system state they both return newElevatorControlSystem
instance, the old instance remainsunchanged
. -
You can chain several calls:
controlSystem .pickup(...) .pickup(...) .step() .step() .status()
To set initial state - call ElevatorSimulator
constructor:
val controlSystem = ElevatorSimulator(List(
(ElevatorId(1), FloorNumber(0), Up, Set(FloorNumber(1))),
(ElevatorId(2), FloorNumber(-1), Down, Set(FloorNumber(-2))),
...
))
Notes:
- at least
one
elevator should be provided, maximum is16
FloorNumber
should be between-9
and9
- for simplicity elevators has
unlimited
capacity (number of people)
Then challenge is to schedule and process the pickup requests. There are few different options, though:
-
First requests handled first,
FCFS
(first-come, first-served). But, it may lead to both:- inefficient elevator usage, and
- delays of requests
Elevators may go to the far floors first, ignoring more close but less priority requests.
-
Schedule requests for elevators that are:
- the closest to the pickup floor, and
- going the same direction has a priority
Ofcourse, there are many other possible options and optimisations. They are not considered here for the simplicity of the solution.
For handling request in more natural and good enough way,
the 2nd
option was chosen.
The pickup requests are scheduled at the moment of pickup request
,
by adding pickupFloor
to the goalFloors
of the closest
elevator with giving priority to the elevators going
the same direction
as pickup direction
.
|
Pickups | Elevator 1 | Elevator 2 |
---|---|---|---|
initial |
0 Up (2) | 0 Down (-2) | |
pickup(1, 3) |
1 -> (3 ) |
0 Up (1 , 2) |
0 Down (-2) |
pickup(-1, -3) |
1 -> (3), -1 -> (-3 ) |
0 Up (1, 2) | 0 Down (-1 , -2) |
step() |
1 Up (2, 3 ) |
-1 Down (-2, -3 ) |
|
step() |
2 Up (3) |
-2 Down (-3) |
|
step() |
3 Up |
-3 Down |