Interview Query

Closest Key

Have you seen this question before?

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.



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


closest_key(dictionary, input) -> 'm'

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

Next question: Find the missing number
Python 3.9.6
Loading editor
Use Shift + Enter to run code