Test your foundational knowledge on sorting algorithms, choosing sort keys, binary search, and two-pointer strategies. This quiz covers the basics of sorting and searching in computer science, helping you reinforce key concepts with clear examples.
When sorting a list of students by their exam scores in descending order, which attribute should you use as the sort key?
Explanation: The correct attribute for sorting students by exam results is their exam score, as it directly reflects the desired order. Sorting by student name or ID would organize the list in ways unrelated to academic performance. Sorting by date of birth is useful for age-based arrangements but irrelevant for exam score lists. Using the right sort key ensures the sorting is meaningful for the task.
What must be true about a list before you can apply binary search to find a number efficiently?
Explanation: Binary search requires the data to be sorted because it works by repeatedly dividing the range in half based on order. Having unique values is not essential, as duplicates do not affect the process. Being in reverse order is only acceptable if you adjust the binary search logic, and even length is not a requirement at all.
Which problem is commonly solved efficiently using the two-pointer technique after sorting: finding two numbers in an array that add up to a target value?
Explanation: The two-pointer approach is effective for finding pairs with a specific sum after sorting an array, allowing you to move pointers inward based on the sum. Sorting is usually required for this strategy to work efficiently. The technique is not limited to duplicate values or unsorted arrays. Suggesting that sorting is not needed misunderstands when the two-pointer method is optimal.
Which sorting property ensures that equal elements remain in their original relative order after sorting?
Explanation: Stability ensures that duplicate items retain their initial sequence, which is important for certain applications. Efficiency refers to speed or resource usage, not element position. Complexity describes algorithmic behavior, and scalability relates to handling larger data. Only stability directly addresses relative order preservation among equal items.
If a list of numbers is randomly ordered, what is the main benefit of sorting it before performing a binary search?
Explanation: Sorting transforms a random list into an ordered one, which is required for binary search to function efficiently. Sorting does not affect the randomness of data or automatically delete duplicates. The length of the list stays the same, so it does not become shorter.
When you want to sort athletes first by their country and then by their finish time, which method should you use to choose the sort keys?
Explanation: Sorting first by country groups athletes by nation, then sorting by finish time arranges them within those groups by performance. Sorting by finish time only ignores country grouping, while sorting randomly produces no meaningful order. Sorting by name and then by country does not align with the intended goal.
If you use binary search to find the number 7 in the sorted list [2, 4, 7, 10, 13], what is the first number you compare 7 with?
Explanation: Binary search begins by comparing the middle element, which is 7 in this list. Although 2 and 13 are the first and last elements, binary search does not start with them. 10 is to the right of the middle and would be compared only if needed. The correct answer is the actual middle value.
Which basic sorting algorithm is often efficient and easy to implement for very small lists?
Explanation: Insertion sort is simple and fast for sorting small arrays due to its low overhead. Heap sort and radix sort are generally better for larger datasets, and 'Fourier sort' is not a standard sorting algorithm. Thus, insertion sort is the most practical for short lists.
What does it mean if binary search returns -1 or indicates 'not found' when searching for 20 in the sorted list [1, 3, 6, 12, 15]?
Explanation: A 'not found' result shows the searched element does not exist within the list. Being the last item would require 20 to appear, which it does not. Duplicates or lack of sorting are not necessarily implied by this outcome alone. The correct interpretation is that 20 is missing.
When using the two-pointer technique on a sorted array, how do you typically adjust the pointers if the sum of pointed elements is less than the target?
Explanation: If the sum is too small, increasing the lower value by moving the left pointer right helps reach the target. Moving the right pointer right does not make sense as it is already at the high end. Moving both pointers left would decrease the sum, going further from the target if the list is sorted ascending. Moving both to the ends is not necessary unless starting over.