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

Chantal's finished problems wk 1 #1

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

Chantal's finished problems wk 1 #1

wants to merge 1 commit into from

Conversation

ghost
Copy link

@ghost ghost commented Aug 16, 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 the count increases as the size of the array increases
What is the space complexity of the length method? Provide justification. O(1) doesn’t allocate memory
What is the time complexity of the print_array method? Provide justification. O(n) because the time it takes to print is directly correlated with the size of the array
What is the space complexity of the print_array method? Provide justification. O(1) doesn’t allocate memory
What is the time complexity of the reverse method? Provide justification. O(n) because the time it takes to reverse is directly correlated with the size of the array
What is the space complexity of the reverse method? Provide justification. O(1) doesn’t allocate memory. If I were to pass in a bigger array, what I store in temp wouldn't change.
What is the time complexity of the search method? Provide justification. O(n) because the time it takes to search is directly correlated with the size of the array
What is the space complexity of the search method? Provide justification. O(1) doesn’t allocate memory
What is the time complexity of the delete method? Provide justification. O(n) because the time it takes to search/delete is directly correlated with the size of the array
What is the space complexity of the delete method? Provide justification. O(1) doesn’t allocate memory
What is the time complexity of the empty method? Provide justification. O(n) because the time it takes to empty is directly correlated with the size of the array
What is the space complexity of the empty method? Provide justification. O(1) doesn’t allocate memory
What is the time complexity of the find_largest method? Provide justification. O(n) because the time it takes to search is directly correlated with the size of the array
What is the space complexity of the find_largest method? Provide justification. O(1) doesn’t allocate memory
What is the time complexity of the insert_ascending method? Provide justification. O(n) because the time it takes to search is directly correlated with the size of the array
What is the space complexity of the insert_ascending method? Provide justification. O(1) doesn’t allocate memory


until array[i] > value_to_insert
if array[i] == SPECIAL_VALUE
return
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, you should be inserting at array[i] and replace SPECIAL_VALUE with value_to_insert.

next_val = array[i + 1]

until i == length - 1
if array[i] == SPECIAL_VALUE
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

temp = next_val
next_val = array[i + 1]

end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point, you should update array[insert_index] to have the value of value_to_insert before returning.

@shrutivanw
Copy link
Collaborator

Looks good! A few comments to check on bugs. It may help to run your code and see the output to confirm if the values are what you expect to see.

In sort, you'll return min instead of sorting the array inside the first loop. I don't think you intend to return min. If you don't return, after the first loop, you'd have found the min element. After the first loop, i will be length -1 and the second loop will never get entered. Think more throught sort - test out your method.

In delete,

  • even in the case you find the value_to_delete, you'd want to increment i, or return.
  • with the current logic, you'll end up fragmenting the array. i.e. if the array was [2, 4, 6] and the value_to_delete was 4, then the array will become [2, SPECIAL_VALUE, 6]. How can you avoid this and have all the SPECIAL_VALUEs in the end?

In find_largest,
(i) The array is unsorted. So let's say the array values happen to be in ascending manner (e.g. [3, 5, 6, 8] then, you'll have array[0], which is 3 is not greater than array[length-1], which is 8. i and j don't get updated and you return 0, which is not correct. The return should be outside the loop. Then you'll need to fix the infinite loop by moving incrementing i to be outside the conditional statement.
(ii) you don't need k.
(iii) you're never comparing the value of current max before updating max. What if the array is [9, 2, 3, 15]? Then max will be 15 after the first iteration, since it's greater of 9 and 15. After than, max will get updated to be 3 in the second iteration.
Overall, think through find_largest again - how would you find the largest value in an unsorted integer array?

In insert_ascending, you're never changing any values in the array to have value_to_insert. If the array is [2, 3, 4, SPECIAL_VALUE] and the value_to_insert is 9, then you'll reach i = 3, where the value is SPECIAL_VALUE. In this case, you shouldn't be updating this value to be value_to_insert before returning. You are missing the updating value.
Also, check if the loop should terminate at i == length - 1 or i == length.

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.

1 participant