FINAL Flashcards
What is overriding?
A subclasses method “hides” the parent’s method with the same name and parameters.
What is overloading?
A class has two methods with same names, but different parameters.
What is a final method?
A method that cannot be overridden.
What is a final class?
A class that cannot have a subclass.
What is a final value?
A value that can only be declared once.
What is a public method?
A method that is visible to all classes.
What is a private method?
A method that is not visible to any classes.
What is a package-private method?
A method that is only visible to classes in the same package.
How do you declare a package-private method?
By not labeling it as any type. (public, private, protected)
What is a protected method?
A method that is visible to the same package and any subclasses of the class it is in.
Why would you mark a method or variable as static?
It belongs to the class, not an instance of the class.
What is the time complexity of searching a LinkedList for an element?
O(n)
What is the time complexity of searching for the first element of a LinkedList?
O(1)
What is the time complexity of retrieving an element from an index in a LinkedList?
O(n)
What is the time complexity of adding/removing to the beginning of a LinkedList?
O(1)
What is the time complexity of adding/removing to a random index of a LinkedList?
O(n)
What is the time complexity of generic Stack methods?
O(1)
What is the average time complexity of generic BST methods?
O(log(n))
What is the worst case time complexity of generic BST methods, and why?
O(n), if all the elements are to one side of the root, it becomes a linked list.
Describe the process of inserting into a LinkedList for all scenarios.
Beginning - set element’s next equal to root and then root equal to element.
Middle - traverse through linked list until you reach right before the index, set element’s next equal to current node’s next, and then set current’s next equal to element.
End - traverse through the linked list until you reach the last element, then set the last element’s index equal to element
Describe the process of removing an element from a LinkedList in all scenarios?
Find the element:
Beginning - set root equal to root’s next
Anywhere - maintain a pointer to the previous element and traverse to the element to be removed, set the previous element’s next to the current element’s next.
Describe the process of searching for an element in a BST.
Recursively, check if the element is equal to the root of the sub tree, if not, if the element is less than the root search the left subtree and if it is more search the right subtree.
Describe the process of inserting an element into a BST.
Recursively, search for where the element should be and once the node you are at is equal to null set it equal to element.
Describe the process of removing an element from a BST.
Find the element then apply one of the following cases:
Leaf node: set the parent’s pointer equal to null
One child: set the parent’s pointer to the child
Two children: replace the element by removing its successor(left-most element in its right subtree)
What are the two ways a stack can be implemented?
Linked List Backing
Array Backing(can overflow)
What is the difference between a queue and a stack?
A queue has the same time complexities as a stack, but a queue is LIFO(last in first out).
What is the time complexity of the insert and remove methods of a Heap?
O(log n)
Describe the process of removing from a Heap.
Replace the root index with the last index of the heap. Then check if the value is out of order and swap with child node until sorted. (depends on if it is a min or a max heap)
What is a Heap backed by?
An array
What’s the formula for accessing the left child of an index in a Binary Heap?
(2 * index) + 1
What’s the formula for accessing the right child of an index in a Binary Heap?
(2 * index) + 2
What’s the formula for accessing the parent of an index in a Binary Heap?
(index - 1) / 2