Learn

In addition to analyzing the runtime of various data structures, it is also important to compare the runtime of different data structures.

In this exercise, we will compare the runtime of retrieving the first value added to a queue to the runtime of retrieving the first value added to a stack.

Kirby Zachariah loves to travel! She’s been to six different countries and often forgets the order in which she visited them. However, knowing she has a bad memory, she decided to keep track of the countries she’s visited in both a Queue and a Stack.

As you can see in the code, the countries have been added both to my_queue and my_stack in the order they were visited.

Instructions

1.

Run the code to see what the value at the front of the queue is and what the value at the top of the stack is.

2.

Change the value of first_value_added_to_queue to the first value added to my_queue. DO NOT simply write "Australia". Use the queue methods to extract the first value. It is ok to remove values from the queue.

3.

Given that N is the size of the initial queue, change the value of queue_runtime to the big O runtime of getting the first value added to the queue. Select from the following options:

  • "1"
  • "N"
  • "log N"
  • "N log N"
  • "N^2"
  • "2^N"
  • "N!"
4.

Change the value of first_value_added_to_stack to the first value pushed onto my_stack. DO NOT simply write "Australia". Use the stack methods to extract the first value. It is ok to remove values from the stack. Check the hint if you want to see how to retrieve the first value added to the stack.

5.

Given that N is the size of the initial stack, change the value of stack_runtime to the big O runtime of getting the first value pushed onto the stack. Select from the following options:

  • "1"
  • "N"
  • "log N"
  • "N log N"
  • "N^2"
  • "2^N"
  • "N!"

Sign up to start coding

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?