# What's the downside to my solution

" Write a function named `max_key` that takes a dictionary named `my_dictionary` as a parameter. The function should return the key associated with the largest value in the dictionary."

The solution is

``````def max_key(my_dictionary):
largest_key = float("-inf")
largest_value = float("-inf")
for key, value in my_dictionary.items():
if value > largest_value:
largest_value = value
largest_key = key
return largest_key
``````

I did not know that you could represent infinity with float(âinfâ) so this is how I solved it:

``````def max_key(my_dictionary):
max_value = sorted(my_dictionary.values())[-1]
return list(my_dictionary.keys())[list(my_dictionary.values()).index(max_value)]
``````

Is there a downside to this solution? Is there a circumstance where it wouldnât work the way it is supposed to? So far it has worked for every dictionary I have passed it. Thanks in advance for your help.

Hmm if youâre looking for a little criticism Iâd say that second option is difficult to read at present (perhaps it could be spread out a little). Thereâs also a lot of wasted processing/memory on temporaries, each list you create for example (with `sorted()` or with `list()`) is going to take a certain amount of processing to create and a certain amount of memory to store.

For a small enough dataset that wouldnât matter but for scaling it could be very important (the loop example takes up little to no memory, a couple of references to pre-existing values and iterates just the once, O(n)).

The other thing that springs to mind is that you might want to sort on something other than numeric types sometimes (depends on the dictionary contents) but they both might struggle a little with that.

This is exactly the type of feedback I was looking for! Can you point me toward more resources that explain this type of thought process. Code academy doesnât really teach this sort of thing. At least I havenât arrived there yet.

1 Like

Ah grand so. Youâd probably find bits of what youâre looking for in the software or comp sci courses (thereâs often some discussion with most âpass the technical interviewâ courses too). Youâre looking for things covering algorithms, Big O / asymptotic notation and probably a bit of data structures too.

Thereâs plenty of resources outside cc for the same but just be aware itâs a very very broad topic. Well studied though so there are some excellent options for getting stuck in. Takes a while but itâs something youâll likely never fully forget and therefore worth the time and effort.

This is great. Thank you so much for all of your help.

1 Like

As a reference text, itâs hard to go wrong with CLRS. Introduction to Algorithms - Wikipedia

And thereâs a free MIT course on youtube that teaches algorithms at a pretty good level (syllabus and exercises are included in pdfâs!): Introduction to Algorithms | Electrical Engineering and Computer Science | MIT OpenCourseWare

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.