Bird
0
0
LLDsystem_design~10 mins

Multiple elevator coordination in LLD - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assign an elevator to a request based on the nearest elevator.

LLD
def assign_elevator(request_floor, elevators):
    nearest = None
    min_distance = float('inf')
    for elevator in elevators:
        distance = abs(elevator.current_floor - [1])
        if distance < min_distance:
            min_distance = distance
            nearest = elevator
    return nearest
Drag options to blanks, or click blank then click option'
Aelevators
Belevator.current_floor
Cmin_distance
Drequest_floor
Attempts:
3 left
💡 Hint
Common Mistakes
Using elevator.current_floor instead of request_floor causes zero distance always.
Using min_distance or elevators in distance calculation is incorrect.
2fill in blank
medium

Complete the code to update the elevator's direction after assigning a new target floor.

LLD
def update_direction(elevator, target_floor):
    if target_floor > elevator.current_floor:
        elevator.direction = [1]
    elif target_floor < elevator.current_floor:
        elevator.direction = 'down'
    else:
        elevator.direction = 'idle'
Drag options to blanks, or click blank then click option'
A'idle'
B'up'
C'down'
D'stop'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting direction to 'idle' or 'down' incorrectly when target floor is higher.
Using undefined direction values.
3fill in blank
hard

Fix the error in the code that checks if an elevator can pick up a request on its way.

LLD
def can_pickup(elevator, request_floor):
    if elevator.direction == 'up' and elevator.current_floor <= [1] <= elevator.target_floor:
        return True
    elif elevator.direction == 'down' and elevator.target_floor <= [1] <= elevator.current_floor:
        return True
    return False
Drag options to blanks, or click blank then click option'
Arequest_floor
Belevator.current_floor
Celevator.target_floor
Delevator.direction
Attempts:
3 left
💡 Hint
Common Mistakes
Using elevator.current_floor or target_floor instead of request_floor in the condition.
Comparing direction as a floor number.
4fill in blank
hard

Fill both blanks to correctly update the elevator's target floors queue and direction after servicing a floor.

LLD
def service_floor(elevator, floor):
    if floor in elevator.target_floors:
        elevator.target_floors.remove([1])
    if not elevator.target_floors:
        elevator.direction = [2]
Drag options to blanks, or click blank then click option'
Afloor
B'idle'
C'stop'
Delevator.current_floor
Attempts:
3 left
💡 Hint
Common Mistakes
Removing elevator.current_floor instead of the serviced floor.
Setting direction to 'stop' which is not a valid state.
5fill in blank
hard

Fill all three blanks to implement a function that selects the best elevator for a request considering direction and distance.

LLD
def select_best_elevator(request_floor, elevators):
    best_elevator = None
    best_score = float('inf')
    for elevator in elevators:
        if elevator.direction == [1] and can_pickup(elevator, request_floor):
            score = abs(elevator.current_floor - [2])
        else:
            score = abs(elevator.current_floor - [3]) + 10
        if score < best_score:
            best_score = score
            best_elevator = elevator
    return best_elevator
Drag options to blanks, or click blank then click option'
A'up'
Brequest_floor
Celevator.target_floor
D'down'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong direction string or floor variables in conditions.
Not adding penalty for elevators that cannot pick up.