FAQ: Asymptotic Notation: Python - HashMaps vs. Linked Lists Runtime

This community-built FAQ covers the “HashMaps vs. Linked Lists Runtime” exercise from the lesson “Asymptotic Notation: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise HashMaps vs. Linked Lists Runtime

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Checkpoint 2 in the excersise says:

hashmap_runtime = “1”

Which I believe is wrong.
Big O stands for worst case, which would be searching for a key that is not in the HashMap. In which case we end up in infinite while loop with the existing code, because:

possible_return_value != key

Always evaluates False because possible_return_value is a list and key is a string

possible_return_value is None

Always evaluates False because our HashMap is full and we have value for every index

possible_return_value[0] == key

Alwayse evaluates False because our key is not a valid key

in place of the infinite while loop we could use

for i in range(self.array_size - 1)

with this for loop in place our big O should look like this

hashmap_runtime = “N”

Is this correct?

Once through N items will be O(N), as you suspect.

Sorry if my question was not formulated correctly. The second excersise in the lesson is asking for big O for HashMap search.

  1. Through the above solution I came up with the answer “N”
  2. The excersise does not accept this as correct answer and it requires answer “1” to advance to excersise 3

Which of the two is correct and is there a bug in the excersise.

Please post a link to this module. Thanks.

https://www.codecademy.com/paths/computer-science/tracks/cspath-asymptotic-notation/modules/cspath-asymptotic-notation/lessons/asymptotic-notation-python/exercises/hashmaps-linkedlist-runtime

1 Like

I haven’t taken this course, but something just occured to me… Does it take the same amount of time to retrieve a item no matter which key we poll in the HashMap? If that is the case, then it would, I believe, be O(1).

HashMaps use collision when assisigning key-value pairs to a list, to avoid writting different keys to same index. When collision occurs the key-value pair is written at the next list index. Without collision we will have O(1) runtime:
1)hash the key
2)compress the hash to an index
3)grab the value at index from the list

With collision we have to iterate the list so it will be O(N):
1)hash the key
2)compress the hash to index
3) if key at index matches our key return value at index
4) else add 1 to collision counter and repeat (1-3)

1 Like

Hi
Could anyone help me understand why this code doesn’t run successfully for exercise 3 on HashMaps vs. Linked Lists Runtime:

#Get Zachary’s Disease from a Linked List
#Write Code here for Checkpoint 3
current = my_linked_list.get_head_node()
if current_value[0] == ‘Zachary’:
linked_list_zachary_disease = current_value[1]
else:
while current.get_next_node != None:
print(current.get_next_node() != None) #this is just here to show myself that this is None on the iteration where the code fails.
if current.get_next_node().get_value()[0] == ‘Zachary’:
linked_list_zachary_disease = current.get_next_node().get_value()[1]
current = current.get_next_node()

I thought the while loop would stop before I get the error:
Traceback (most recent call last):
File “script.py”, line 40, in
if current.get_next_node().get_value()[0] == ‘Zachary’:
AttributeError: ‘NoneType’ object has no attribute ‘get_value’

Any help much appreciated!

It makes your code much more readable if you post it using proper formatting via the </> icon in the menu at the top of the text box.

As to your question, look at these three lines:

while current.get_next_node != None:
    print(current.get_next_node() != None)
    current = current.get_next_node()

Do you see anything in the second and third lines that is missing from the first?

@patrickd314 thanks, I’ll use that formatting next time (first time posting here!)

And yes I do - that’s all working now. I can’t believe how long I stared at this without noticing that.

Thanks again!