Interview Query

Closest Key

Start Timer

0:00:00

Upvote
4
Downvote
Save question
Mark as completed
View comments (24)
Next question

Given a dictionary with keys of letters and values of a list of letters, write a function closest_key to find the key with the input value closest to the beginning of the list.

Example:

Input:

dictionary = {
    'a' : ['b','c','e'],
    'm' : ['c','e'],
}
input = 'c'

Output:

closest_key(dictionary, input) -> 'm'

cc is at distance 1 from aa and 0 from mm. Hence closest key for cc is mm.

.
.
.
.
.


Comments

Loading comments