1.
What is the time complexity of the insert(index) method in ArrayList?
Correct Answer
A. O(n)
Explanation
The time complexity of the insert(index) method in ArrayList is O(n). This means that the time it takes to insert an element at a specific index in an ArrayList grows linearly with the size of the ArrayList. In other words, if the ArrayList has n elements, it will take approximately n operations to insert an element at a specific index.
2.
Indicate constant time complexity in terms of Big-O notation.
Correct Answer
B. O(1)
Explanation
The correct answer is O(1) because it indicates constant time complexity. In Big-O notation, O(1) means that the algorithm's running time does not depend on the input size. It implies that the algorithm takes the same amount of time to execute, regardless of the size of the input. This is the most efficient time complexity as it guarantees a constant and predictable performance.
3.
Indicate exponential time complexity in terms of big-O notation.
Correct Answer
C. O(2^n)
Explanation
Exponential time complexity, represented by O(2^n), means that the time required to solve a problem increases exponentially with the size of the input. This notation indicates that the algorithm's running time doubles with each additional input element. It is the slowest time complexity among the options provided, as it grows very quickly and becomes impractical for larger inputs.
4.
Find the slowest time.
Correct Answer
C. O(n!)
Explanation
The answer O(n!) indicates that the time complexity of the algorithm is factorial, meaning that the runtime grows exponentially with the size of the input. This suggests that the algorithm has a very inefficient and slow performance, as the factorial function grows extremely quickly. In comparison, O(n), O(n^2), and O(2^n) have better time complexities and would be faster than O(n!) for larger inputs.
5.
What is the time complexity of the ArrayList remove(index) method?
Correct Answer
A. O(n)
Explanation
The time complexity of the ArrayList remove(index) method is O(n). This means that the time it takes to remove an element from the ArrayList is directly proportional to the number of elements in the list. The method needs to shift all the subsequent elements to fill the gap left by the removed element, which requires iterating through the remaining elements. Therefore, the time complexity is linear, or O(n).
6.
What is the time complexity of adding an item before a LinkedList?
Correct Answer
B. O(1)
Explanation
The time complexity of adding an item before a LinkedList is O(1). This means that regardless of the size of the LinkedList, the time it takes to add an item before it remains constant. This is because LinkedLists use pointers to connect each node, so adding an item before it simply involves updating the pointers of the new item and the existing item, which can be done in constant time.
7.
What is the time complexity of adding elements at the beginning of ArrayList?
Correct Answer
A. O(n)
Explanation
Adding elements at the beginning of an ArrayList requires shifting all existing elements to the right to make space for the new element. This operation has a time complexity of O(n) because it needs to iterate through all the elements in the ArrayList to shift them. Therefore, the correct answer is O(n).
8.
Indicate logarithm polynomial time complexity.
Correct Answer
A. O(n^const(const=2,3…) )
Explanation
The correct answer is O(n^const(const=2,3…)). This indicates that the algorithm's time complexity is polynomial, specifically a power of n. The notation O(n^const(const=2,3…)) represents a polynomial time complexity where the highest power of n is a constant value (2, 3, etc.). This means that the algorithm's running time grows at a rate proportional to some constant power of the input size n.
9.
What is the time complexity of the insert(index) method in ArrayList?
Correct Answer
A. O(n)
Explanation
The time complexity of the insert(index) method in ArrayList is O(n). This means that the time it takes to insert an element at a specific index in an ArrayList is directly proportional to the number of elements already in the ArrayList. As the number of elements increases, the time taken to insert an element also increases linearly.
10.
What is the time complexity of the recursive Binary Search algorithm?
Correct Answer
C. O(logn)
Explanation
The time complexity of the recursive Binary Search algorithm is O(logn) because in each recursive call, the algorithm divides the search space in half, reducing the number of elements to search by half each time. This results in a logarithmic time complexity as the search space is continually halved until the target element is found or the search space becomes empty. Therefore, the time complexity of the recursive Binary Search algorithm is O(logn).
11.
What is the time complexity of the linear search algorithm?
Correct Answer
A. O(n)
Explanation
The time complexity of the linear search algorithm is O(n) because it has to iterate through each element in the worst case scenario. This means that the time it takes to execute the algorithm increases linearly with the size of the input.
12.
Search a binary search tree costs?
Correct Answer
C. O(logn)
Explanation
The search operation in a binary search tree has a time complexity of O(logn). This is because in a binary search tree, the elements are arranged in a specific order, allowing for efficient searching. At each step of the search, the current node is compared with the target value, and based on the result, the search continues either in the left or right subtree. Since the tree is balanced and each comparison reduces the search space by half, the time complexity of the search operation is logarithmic in the number of elements in the tree.
13.
Element insertion to a Binary Search tree costs?
Correct Answer
C. O(logn)
Explanation
The correct answer is O(logn) because when inserting an element into a binary search tree, we start from the root and compare the value of the element with the current node. Based on the comparison, we move either to the left or right subtree. This process is repeated until we find an empty spot to insert the element. Since at each step we divide the search space in half, the time complexity of element insertion in a binary search tree is logarithmic, making it O(logn).
14.
Insert and remove items from a heap costs?
Correct Answer
C. O(logn)
Explanation
Inserting and removing items from a heap typically costs O(log n) time complexity, where "n" is the number of elements in the heap. This is because heaps are typically implemented as binary trees (binary min-heap or max-heap), and in a well-implemented heap, the height of the tree is logarithmic in the number of elements. So, the correct answer is O(log n).
15.
The average time complexity of the Selection sort is?
Correct Answer
B. N^2
Explanation
The average time complexity of Selection Sort is O(n^2), where "n" represents the number of elements in the array. This complexity arises because, in each pass of the algorithm, it searches for the minimum (or maximum) element from the unsorted portion and swaps it with the element in the current position, resulting in a quadratic time complexity.
16.
What is the average time complexity of the Heap sort?
Correct Answer
D. O(nlogn)
Explanation
The average time complexity of Heap sort is O(nlogn). This means that the time taken to sort a list of n elements using Heap sort will increase in proportion to n multiplied by the logarithm of n. This complexity arises from the fact that Heap sort involves building a heap and then repeatedly extracting the maximum element from the heap and re-heapifying the remaining elements. The process of building the heap takes O(n) time, and the extraction and re-heapification process is repeated n times, each taking O(logn) time. Thus, the overall average time complexity is O(nlogn).
17.
The average time complexity of Quicksort is?
Correct Answer
D. O(nlogn)
Explanation
Quicksort is a sorting algorithm that divides the input array into two smaller sub-arrays, and recursively sorts these sub-arrays. The pivot element is chosen and the elements are partitioned around it. The average time complexity of Quicksort is O(nlogn) because in each recursion, the algorithm divides the array into two parts, which takes O(logn) time. Additionally, the partitioning step takes O(n) time in the average case. Therefore, the overall time complexity is O(nlogn).
18.
The average time complexity of Insertion sort is?
Correct Answer
B. O(n^2)
Explanation
Insertion sort is a simple sorting algorithm that works by repeatedly inserting elements into their correct positions within a sorted subarray. It iterates through the array, comparing each element with the elements before it and shifting them to the right if they are greater. The average time complexity of Insertion sort is O(n^2) because in the worst-case scenario, when the array is in reverse order, it would require comparisons and shifts for each element, resulting in a quadratic time complexity.
19.
A hash table uses hashing to transform an item's key into a table index so that iterations, retrievals, and deletions can be performed in expected ___________ time.
Correct Answer
C. O(1)
Explanation
A hash table uses hashing to transform an item's key into a table index so that iterations, retrievals, and deletions can be performed in expected constant time. This means that regardless of the size of the hash table or the number of items stored in it, the time it takes to perform these operations remains constant. Therefore, the correct answer is O(1).
20.
The average time complexity of Merge sort is?
Correct Answer
D. O(nlogn)
Explanation
Merge sort has a time complexity of O(nlogn) because it divides the array into two halves recursively, sorts them separately, and then merges them back together. The time complexity of merging two sorted arrays of size n/2 each is O(n), and since the array is divided in half at each step, the total time complexity is O(nlogn).
21.
The average time complexity of Shell sort is?
Correct Answer
C. O(n^1.25)
Explanation
Shell sort is an efficient sorting algorithm that improves upon the time complexity of insertion sort. It achieves this by sorting elements that are far apart before progressively reducing the gap between elements to be sorted. The average time complexity of Shell sort is O(n^1.25), which means that the time it takes to sort a list of n elements increases at a slower rate than n^2 but faster than n. This makes Shell sort faster than insertion sort, but slower than more advanced sorting algorithms like quicksort or mergesort.
22.
The average time complexity of Bubble sort is?
Correct Answer
A. O(n^2)
Explanation
Bubble sort is a simple sorting algorithm that repeatedly steps through the list, and compares adjacent elements, and swaps them if they are in the wrong order. The algorithm continues to do this until the entire list is sorted. In the worst-case scenario, where the list is in reverse order, Bubble sort will have to make n-1 passes through the list, each time comparing and swapping adjacent elements. This results in a time complexity of O(n^2), where n is the number of elements in the list.