- What are the pros and cons of immutability?
It can be helpful to have an object be immutable, to avoid causing bugs in complex code that references the object over and over. The con is in it’s static form, you can’t change it, and have to create new data structures to make any changes.
2.How can you achieve immutability in your own code?
3. What are Divide and Conquer algorithms? Describe how they work. Can you give any common examples of the types of problems where this approach might be used?
“A divide-and-conquer algorithm works by recursively breaking down a problem into two or more sub-problems of the same or related type, until these become simple enough to be solved directly. The solutions to the sub-problems are then combined to give a solution to the original problem.”
I think it was said best in the wiki article. Sometimes the jargon is a little confusing, but in this case I think you could read it without an extensive coding vocabulary and understand it.
4. How do insertion sort, heap sort, quick sort, and merge sort work?
Insertion sort is extremely simple, and very ineffective in larger data structures, but it’s simplicity can make it somewhat useful in smaller structures. It works by starting with the beginning of the array, comparing the first two objects, then the first three, then first four and so on, until it’s completed.
Heap sort works by having a sorted array, and an unsorted array. It first builds a sorted array the size of the unsorted array, places the largest item at the root of the sorted array, replace that with the last item of the unsorted array, then some other stuff. Should probably look at this again when I have more time.
Quicksort is thought of as more efficient than the two I just listed. It is a divide and conquer type of algorithm, and works by selecting a pivot element, and places smaller elements in one half of the array, while placing larger ones on the other half, and with each element, sorts them recursively. This method of sorting can be three times as fast as the other sorting methods talked about when implemented well.
Mergesort is generally a more efficient type of sorting algorithm, and has a more widespread application than quicksort. It is another divide and conquer algorithm, where you divide the list into the smallest unit, compare each element with the adjacent list, merge the two and repeat till finished.
5. Explain the difference between mutable and immutable objects.
You can change an object/ you can’t change an object.
6. What are the three laws of algorithm recursion? Describe them in your own words and what they mean to you.
The algorithm needs a base case, which is essentially a condition for recursion to end (meaning the function/process can continue to call upon itself).
Through each recursion, it needs to move closer to the base case, meaning that whatever you are modifying, lets say an array you want to sort, it needs to get closer to being sorted through each start of recursion.
The third is that it must be recursive, again meaning that the algorithm calls upon itself.