What is the time complexity for quicksort, mergesort, and a hash-based lookup in Big-O terms?

Prepare for the DDR Data Science Interview Test. Access flashcards and multiple choice questions, each with detailed hints and explanations. Enhance your readiness for the interview!

Multiple Choice

What is the time complexity for quicksort, mergesort, and a hash-based lookup in Big-O terms?

Explanation:
Quicksort’s time grows like n log n on average because each partition step splits the data roughly in half and you repeat this across about log n levels, with O(n) work at each level. If the pivot is consistently the smallest or largest element, you get a worst-case pattern that yields O(n^2). Mergesort also runs in n log n time consistently because it always divides the input in half and then merges the halves in linear time, across about log n levels, without depending on the input order. Hash-based lookup shines with average constant time search, O(1), thanks to indexing by a hash function. Building the table from n items takes O(n) on average. While collisions can affect performance in worst cases, the typical assumption is that lookups are constant time on average and building is linear. So the combination matches: quicksort averages to O(n log n) with a worst case of O(n^2); mergesort is O(n log n); and hash-based lookup is O(1) average for lookups with O(n) to build.

Quicksort’s time grows like n log n on average because each partition step splits the data roughly in half and you repeat this across about log n levels, with O(n) work at each level. If the pivot is consistently the smallest or largest element, you get a worst-case pattern that yields O(n^2).

Mergesort also runs in n log n time consistently because it always divides the input in half and then merges the halves in linear time, across about log n levels, without depending on the input order.

Hash-based lookup shines with average constant time search, O(1), thanks to indexing by a hash function. Building the table from n items takes O(n) on average. While collisions can affect performance in worst cases, the typical assumption is that lookups are constant time on average and building is linear.

So the combination matches: quicksort averages to O(n log n) with a worst case of O(n^2); mergesort is O(n log n); and hash-based lookup is O(1) average for lookups with O(n) to build.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy