🚀 Coding Interview Questions with Answers (Part 10)
9️⃣1️⃣ What is Quick Sort?
Answer:
Quick Sort is a Divide and Conquer sorting algorithm that selects a pivot element and partitions the array so that elements smaller than the pivot are placed on its left and larger elements on its right. The process is then repeated recursively for the left and right subarrays.
Time Complexity:
• Best Case: O(n log n)
• Average Case: O(n log n)
• Worst Case: O(n²) (when the pivot selection is poor)
Advantages:
• Fast in practice
• In-place sorting (requires little extra memory)
• Widely used for large datasets
9️⃣2️⃣ What is Bubble Sort?
Answer:
Bubble Sort repeatedly compares adjacent elements and swaps them if they are in the wrong order. This process continues until the array is sorted.
Time Complexity:
• Best Case: O(n) (optimized version)
• Average Case: O(n²)
• Worst Case: O(n²)
Advantages:
• Easy to understand and implement.
Disadvantages:
• Inefficient for large datasets.
9️⃣3️⃣ What is Insertion Sort?
Answer:
Insertion Sort builds the sorted array one element at a time by inserting each new element into its correct position.
Time Complexity:
• Best Case: O(n)
• Average Case: O(n²)
• Worst Case: O(n²)
Advantages:
• Simple implementation
• Efficient for small or nearly sorted datasets
• Stable sorting algorithm
9️⃣4️⃣ What is Selection Sort?
Answer:
Selection Sort repeatedly finds the smallest element from the unsorted portion of the array and places it at the beginning.
Time Complexity:
• Best Case: O(n²)
• Average Case: O(n²)
• Worst Case: O(n²)
Advantages:
• Simple to implement
• Performs fewer swaps compared to Bubble Sort
9️⃣5️⃣ What is Heap Sort?
Answer:
Heap Sort is a comparison-based sorting algorithm that uses a Binary Heap data structure.
Steps:
1. Build a Max Heap.
2. Swap the root with the last element.
3. Reduce the heap size.
4. Heapify the remaining elements.
5. Repeat until the array is sorted.
Time Complexity:
• Best Case: O(n log n)
• Average Case: O(n log n)
• Worst Case: O(n log n)
Advantages:
• Guaranteed O(n log n) performance
• In-place sorting algorithm
9️⃣6️⃣ What is Counting Sort?
Answer:
Counting Sort is a non-comparison-based sorting algorithm that counts the occurrences of each element and uses these counts to determine their correct positions.
Time Complexity: O(n + k)
Where:
• n = Number of elements
• k = Range of input values
Advantages:
• Extremely fast for small ranges
• Stable sorting algorithm
Limitation:
• Not suitable when the range of values is very large.
9️⃣7️⃣ What is Radix Sort?
Answer:
Radix Sort sorts numbers digit by digit, starting from either the least significant digit (LSD) or the most significant digit (MSD).
It commonly uses Counting Sort as the intermediate sorting algorithm.
Time Complexity: O(n × d)
Where:
• n = Number of elements
• d = Number of digits
Advantages:
• Very efficient for sorting integers and strings with fixed lengths.
Post #2766
2.08K
- ❤ 5