Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alison Z implemented arrays #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Alison Z implemented arrays #15

wants to merge 1 commit into from

Conversation

AlisonZ
Copy link

@AlisonZ AlisonZ commented Aug 17, 2017

Restricted Array

Congratulations! You're submitting your assignment.

Comprehension Questions

What is the time and space complexity for each method you implemented? Provide justification.

Question Answer
What is the time complexity of the length method? Provide justification. O(n) because it is based on the size of the array
What is the space complexity of the length method? Provide justification. O(1) because it will always return 1 integer
What is the time complexity of the print_array method? Provide justification. O(n) because it is based on the size of the array
What is the space complexity of the print_array method? Provide justification. O(n) - based on the size of the array
What is the time complexity of the reverse method? Provide justification. O(n) - based on the size of the array
What is the space complexity of the reverse method? Provide justification. Oops - i wrote reverse! which replaces the original data structure, making this one O(1)
What is the time complexity of the search method? Provide justification. O(n) - based on the size of the array
What is the space complexity of the search method? Provide justification. O(1) - always returns true or false
What is the time complexity of the delete method? Provide justification. O(n2) - because I wrote it with a nested loop
What is the space complexity of the delete method? Provide justification. O(n) - based on the size of the array
What is the time complexity of the empty method? Provide justification. O(n) - based on the size of the array
What is the space complexity of the empty method? Provide justification. O(n) - based on size of the array
What is the time complexity of the find_largest method? Provide justification. O(n) - based on size of the array
What is the space complexity of the find_largest method? Provide justification. O(1) - always returns one
What is the time complexity of the insert_ascending method? Provide justification. O(n2) - I wrote it with a nested loop
What is the space complexity of the insert_ascending method? Provide justification. O(n) - based on the size of the array

@shrutivanw
Copy link
Collaborator

Alison, let me know if you'd like to chat further in person on any of these. I've some verbose feedback for some code logic and some complexity explanations. Also, it might help you spot some of these if you run the code yourself and see the console output - check if it matches what you expect.

  • Generic call out: You're coding in Ruby which does not support i++, at least as far as I know. Change all these to i += 1. I recommend running your code to see if the output aligns with what you expect.

  • In print_array method: Compare to nil and not 0, like you did with length method. 0 could be a legitimate value.
    In reverse method: j, i.e. the right most element in the array will be at array.length - 1 and not array.length + 1. This is because indexing starts at 0 and the index of the last item will be array.length - 1.

  • Space complexity for print_array will be O(1). The amount of additional space allocated doesn't depend on array size. The only thing you are allocating is the variable i. Apart from that,you're just outputting it to the console.

  • In sort, should the inner loop end sooner than array.length? You are looking to insert array[i+1] in the right location between index 0 and index i. So you can end the inner loop at i.

  • In delete - although you have a nested loop, the total number of times the outer loop gets executed in based on i which ranges from 0 to arrray.length-1. The inner loop gets executed on certain conditions, and when it does, i gets incremented within it. So overall, the total nested loop gets executed only n times. And hence the time complexity of delete is O(n).

  • In the delete code, you allocate i, spaces and moved. 3 values that don't change their size based on input array size. So, the space complexity for delete will not depend on input size. Shouldn't be O(n) and instead be O(1).

  • In empty, you only allocate i. This does not change based on array length and hence, the space complexity for empty should be O(1).

  • In find_largest, you are incrementing i twice if array[i] > big and skipping checking a value. You don't need to increment i if the condition is satisfied. Just update big if the condition is satisfied.

  • In insert_ascending, the last element is at index array.length - 1 and not at index array.length. The first conditional statement will need updating based on this. If you update array[i] to be array[i+1], you've just lost the value at array[i]. Also, currently you're not inserting value_to_insert anywhere.

  • I think you want to do the reverse in the reverse order i.e. start at index j = array.length-1. Update array[j] to array[j-1] and so on until you reach i. Since array[array.length -1] will be SPECIAL_VALUE, that's okay to override. Finally, when you reach i, update array[i] to value_to_insert, and then return.
    Once you correct the code logic, you'll get a better idea for time complexity. Similar to the delete code, the inner loop increments i as well and only gets run once. In fact, it doesn't need to be a loop- just a conditional statement. The overall time complexity should be O(n). See if you can get it with updated code logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants