Interview Questions on Search & Sort Methods (C Language)

📘 Basic Search & Sort Concepts – Short Answer Interview Questions

  1. What is the difference between linear search and binary search?
    Linear search checks each element sequentially; binary search requires sorted data and divides search space in half each time.
  2. What is the time complexity of linear search?
    O(n) in worst case where n is number of elements.
  3. When would you choose bubble sort over other sorting algorithms?
    Rarely used in practice, but might be chosen for educational purposes or when simplicity is more important than performance.
  4. What is the advantage of merge sort over quick sort?
    Merge sort has guaranteed O(n log n) performance and is stable, while quick sort can degrade to O(n²) in worst case.
  5. How does binary search work?
    Compare target with middle element, then search left or right half recursively until element is found or search space is exhausted.
  6. What is the time complexity of binary search?
    O(log n) where n is number of elements.
  7. What is a stable sorting algorithm?
    One that preserves the relative order of equal elements (e.g., merge sort, bubble sort).
  8. What is the difference between selection sort and insertion sort?
    Selection sort finds min/max and swaps; insertion sort builds sorted portion by inserting each element in correct position.
  9. When would you use counting sort?
    When input contains integers in small range - O(n+k) where k is range of input.
  10. What is the space complexity of quick sort?
    O(log n) for recursive calls in best/average case, O(n) in worst case.
  11. How does radix sort work?
    Sorts numbers digit by digit from least significant to most significant using a stable sort (often counting sort).
  12. What is the best sorting algorithm for nearly sorted data?
    Insertion sort (O(n) time for nearly sorted data).
  13. What is the worst-case time complexity of quick sort?
    O(n²) when poor pivot choices are made (e.g., already sorted data with first/last element as pivot).
  14. How can you implement binary search recursively?
    Base case: empty search space. Recursive case: compare middle element, then search left or right half.
  15. What is interpolation search?
    Improved binary search for uniformly distributed data that estimates position of target value.
  16. What is the difference between internal and external sorting?
    Internal sorts data in RAM; external sorts data too large for RAM (uses disk).
  17. What is the time complexity of merge sort?
    O(n log n) in all cases (best, average, worst).
  18. How does heap sort work?
    Builds a max heap, repeatedly extracts maximum element to sort in ascending order.
  19. What is the advantage of quick sort over merge sort?
    Typically faster in practice due to better cache performance and smaller constant factors.
  20. What is the lower bound for comparison-based sorting algorithms?
    Ω(n log n) - no comparison-based sort can be faster than this in worst case.
  21. How can you search in a rotated sorted array?
    Modified binary search that checks which half is properly sorted and adjusts search accordingly.
  22. What is the Dutch national flag problem?
    Sorting an array of 0s, 1s, and 2s in linear time - basis for 3-way quick sort partition.
  23. How does shell sort work?
    Generalization of insertion sort that sorts elements far apart first, progressively reducing the gap.
  24. What is the advantage of non-comparison sorts?
    Can achieve O(n) time complexity when data has specific properties (e.g., limited range).
  25. How would you implement a stable version of quick sort?
    Use 3-way partitioning or modify partition to preserve relative order of equal elements.
  26. What is the time complexity of finding the kth smallest element using quick select?
    O(n) average case, O(n²) worst case.
  27. How does jump search work?
    Jumps fixed steps ahead, then performs linear search when overshoot is detected - O(√n) complexity.
  28. What is exponential search?
    Finds range where element may exist by doubling index, then performs binary search in that range.
  29. How can you sort a linked list efficiently?
    Merge sort is ideal for linked lists (O(1) space for recursive version, O(n log n) time).
  30. What is the difference between top-down and bottom-up merge sort?
    Top-down is recursive; bottom-up is iterative and merges small subarrays first.

📗 Advanced Search & Sort Techniques – Short Answer Interview Questions

  1. How would you implement a hash table for fast search?
    Use array with hash function, handle collisions with chaining or open addressing.
  2. What is the time complexity of ternary search?
    O(log₃n), though in practice similar to binary search due to constants.
  3. How can you sort 1 million 32-bit integers with 2MB RAM?
    Use external merge sort - divide into chunks that fit in RAM, sort individually, then merge.
  4. What is the median-of-three technique in quick sort?
    Choosing pivot as median of first, middle, and last elements to avoid worst-case scenarios.
  5. How would you find all duplicates in a sorted array?
    Linear scan comparing each element with next one - O(n) time.
  6. What is the advantage of introsort?
    Hybrid of quick sort, heap sort, and insertion sort that avoids quick sort's worst case.
  7. How can you search in an infinite sorted array?
    Exponential search to find bounds, then binary search.
  8. What is the time complexity of finding the intersection of two sorted arrays?
    O(m+n) using merge-like approach where m and n are array lengths.
  9. How does timsort work?
    Hybrid of merge sort and insertion sort that finds natural runs in data for efficiency.
  10. What is the space complexity of in-place merge sort?
    O(1) for linked lists, O(n) for arrays due to need for temporary storage.
  11. How would you implement a priority queue for efficient sorting?
    Using a heap structure (O(log n) insertion/extraction, O(n log n) sorting).
  12. What is the advantage of B-tree for external sorting?
    Minimizes disk I/O by keeping height low and packing many keys in each node.
  13. How can you find the most frequent element in a sorted array?
    Linear scan keeping track of current element count and maximum found - O(n) time.
  14. What is the advantage of randomized quick sort?
    Random pivot selection makes worst-case scenario extremely unlikely.
  15. How would you sort numbers with a large range but few distinct values?
    Counting sort if range is known and reasonable, or bucket sort.
  16. What is the complexity of finding the closest pair in a sorted array?
    O(n) by comparing consecutive elements in the sorted array.
  17. How can you search in a 2D sorted matrix?
    Start from top-right or bottom-left corner, eliminate row or column at each step - O(m+n).
  18. What is the advantage of using sentinel values in merge sort?
    Eliminates need to check for subarray exhaustion during merge, simplifying code.
  19. How would you implement a hybrid sorting algorithm?
    Combine quick sort (for large partitions) with insertion sort (for small partitions).
  20. What is the best sorting algorithm when memory writes are expensive?
    Selection sort (O(n) writes) or cycle sort (minimum possible writes).