CodeSignal Technical Assessment Sorting and Searching Algorithms Questions and Answers 1 — Questions and Answers
Question 1: A developer needs to search for an element within a large, sorted array of one million integers. Which algorithm offers the best worst-case time complexity for this task?
- Linear Search
- Binary Search (Correct answer)
- Merge Sort
- Quick Sort
Correct answer: Binary Search
Binary Search is the most efficient algorithm for searching in a sorted array. It works by repeatedly dividing the search interval in half. Its worst-case time complexity is O(log n), which is significantly better than Linear Search's O(n). Merge Sort and Quick Sort are sorting algorithms, not searching algorithms.
Question 2: You are tasked with sorting a list of customer objects based on their purchase date. If two customers have the same purchase date, their original relative order must be preserved. Which of the following sorting algorithms is most suitable for this requirement?
- Heap Sort
- Quick Sort
- Selection Sort
- Merge Sort (Correct answer)
Correct answer: Merge Sort
The requirement to preserve the relative order of elements with equal keys means a stable sorting algorithm is needed. Among the choices, Merge Sort is a stable algorithm. Heap Sort, Quick Sort, and Selection Sort are all unstable sorting algorithms and do not guarantee that the original order of equal elements will be maintained.
Question 3: What is the worst-case time complexity of the Quick Sort algorithm, and under what condition does it typically occur?
- O(n log n), when the pivot elements consistently divide the array into two halves of nearly equal size.
- O(n^2), when the input array is already sorted and the pivot is chosen as the first or last element. (Correct answer)
- O(n), when the input array is completely random.
- O(log n), when the input array contains many duplicate elements.
Correct answer: O(n^2), when the input array is already sorted and the pivot is chosen as the first or last element.
The worst-case time complexity of Quick Sort is O(n^2). This scenario happens when the pivot selection consistently results in highly unbalanced partitions, for example, when one partition has n-1 elements and the other has 0. This commonly occurs if the first or last element is chosen as the pivot and the array is already sorted or reverse-sorted.
Question 4: A programmer is working on a memory-constrained embedded system and needs to sort an array of sensor readings in-place. Which of the following sorting algorithms has the best auxiliary space complexity for this task?
- Merge Sort
- Radix Sort
- Heap Sort (Correct answer)
- Tim Sort
Correct answer: Heap Sort
Heap Sort is an in-place sorting algorithm with a space complexity of O(1), meaning it requires a constant amount of extra memory regardless of the input size. Merge Sort requires O(n) auxiliary space, making it unsuitable for this scenario. Radix and Tim Sort can also have higher space requirements.
Question 5: Under which of the following conditions would Insertion Sort likely outperform Quick Sort?
- When sorting a very large, randomly ordered dataset.
- When the dataset is small or nearly sorted. (Correct answer)
- When the dataset contains many floating-point numbers.
- When a stable sort is not required.
Correct answer: When the dataset is small or nearly sorted.
Insertion Sort has a time complexity of O(n) in its best case (an already sorted array) and performs very well on small or nearly sorted arrays due to its low overhead. Quick Sort has an average-case complexity of O(n log n) but has higher overhead, making it less efficient for small datasets.
Question 6: Which of the following is a fundamental prerequisite for the Binary Search algorithm to function correctly?
- The array must not contain any duplicate values.
- The array must be sorted. (Correct answer)
- The array must have a length that is a power of two.
- The array must only contain positive integers.
Correct answer: The array must be sorted.
The core principle of Binary Search relies on dividing the search space in half based on comparing the target value to the middle element. This process only works if the array is sorted, allowing the algorithm to correctly discard one half of the elements in each step.
A developer needs to search for an element within a large, sorted array of one million integers.
Which algorithm offers the best worst-case time complexity for this task?