chapter
    Searching, Sorting, Selection and Hashing PYQs for GATE CS

    Solve 10+ Searching, Sorting, Selection and Hashing previous year questions for GATE CS with answers and detailed solutions. Free sample questions below.

    Try a question

    Answer it here to see how it works. Nothing is recorded until you sign in.

    Question 1
    2026 Slot Set2 PYQ
    Level 3: Exam Standard
    Consider an array . Suppose the merge sort algorithm is executed on array to sort it in increasing order. The merge sort algorithm will carry out a total of 7 merge operations.

    A merge operation on sorted left array and sorted right array is said to be void if the output of the merge operation is the elements of array followed by the elements of array .

    The number of void merge operations among these 7 merge operations is __________. (answer in integer)
    Question 2
    2026 Slot Set2 PYQ
    Level 3: Exam Standard
    Consider an array of integers of size . The indices of run from to . An algorithm is to be designed to check whether satisfies the condition given below.


    Which one of the following gives the worst case time complexity of the fastest algorithm that can be designed for the problem?
    Question 3
    2025 Slot Set2 PYQ
    Level 3: Exam Standard
    Consider an unordered list of distinct integers.

    What is the minimum number of element comparisons required to find an integer in the list that is NOT the largest in the list?
    Question 4
    2025 Slot Set2 PYQ
    Level 3: Exam Standard
    An array of length with distinct elements is said to be bitonic if there is an index such that is sorted in the non-decreasing order and is sorted in the non-increasing order.

    Which ONE of the following represents the best possible asymptotic bound for the worst-case number of comparisons by an algorithm that searches for an element in a bitonic array ?
    Question 5
    2025 Slot Set1 PYQ
    Level 3: Exam Standard
    The pseudocode of a function fun() is given below:

    fun(int A[0,…,n-1]){
    for i=0 to n-2
    for j=0 to n-i-2
    if (A[j]>A[j+1])
    then swap A[j] and A[j+1]

    }

    Let be an array storing 30 distinct integers in descending order. The number of swap operations that will be performed, if the function fun() is called with as argument, is __________. (Answer in integer)
    Question 6
    2024 Slot Set1 PYQ
    Level 3: Exam Standard

    Given an integer array of size , we want to check if the array is sorted (in either ascending or descending order). An algorithm solves this problem by making a single pass through the array and comparing each element of the array only with its adjacent elements. The worst-case time complexity of this algorithm is

    Question 7
    2023 PYQ
    Level 3: Exam Standard
    An algorithm has to store several keys generated by an adversary in a hash table. The adversary is malicious who tries to maximize the number of collisions. Let be the number of keys, be the number of slots in the hash table, and .

    Which one of the following is the best hashing strategy to counteract the adversary?
    Question 8
    2021 Slot Set2 PYQ
    Level 3: Exam Standard

    What is the worst-case number of arithmetic operations performed by recursive binary search on a sorted array of size ?

    Question 9
    2021 Slot Set1 PYQ
    Level 3: Exam Standard
    Consider the following array.

    2332456972738997

    Which algorithm out of the following options uses the least number of comparisons (among the array elements) to sort the above array in ascending order?
    Question 10
    2021 Slot Set1 PYQ
    Level 3: Exam Standard

    Let be an array containing integers. Let be the lowest upper bound on the number of comparisons of the array elements, required to find the minimum and maximum values in an arbitrary array of elements. Which one of the following choices is correct?

    Free preview ends here

    Login to view the complete previous-year questions and solutions

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.

    Searching, Sorting, Selection and Hashing PYQs for GATE CS

    Solve 10+ Searching, Sorting, Selection and Hashing previous year questions for GATE CS with answers and detailed solutions. Free sample questions below.

    Chapter Roadmap: Searching, Sorting, Selection and Hashing

    Chapter Roadmap: Searching, Sorting, Selection and Hashing

    Your journey through this chapter:

    1. Sorting Algorithms and Operation Counts Master exact comparison and swap counts for Bubble, Insertion, Selection, Merge, and Quick sort.
    2. Binary and Bitonic Array Searching Achieve logarithmic time search in sorted and bitonic arrays.
    3. Selection and Comparison Bounds Learn the theoretical minimum comparisons required for selection problems.
    4. Linear-Time Array Property Verification Design single-pass algorithms to verify specific array conditions.
    5. Universal Hashing and Adversarial Inputs Choose hash functions that minimize collisions against malicious inputs.

    What you will master: Trace any sorting or searching algorithm, count its exact operations, and identify the most efficient approach for any data distribution.

    Sorting Algorithms and Operation Counts

    Sorting Algorithms and Operation Counts

    Why this matters: Sorting is not just about getting a sorted array; it is about understanding the computational cost of rearranging data. Different algorithms pay this cost in different ways: some through excessive comparisons, others through excessive data movement.

    What you will learn here:

    • How to derive exact comparison and swap counts for standard sorting algorithms.
    • The relationship between array inversions and sorting operation counts.
    • How to identify "void" operations in divide-and-conquer sorting.
    • Common pitfalls in analyzing loop bounds and operation totals.

    The core idea: An algorithm's efficiency on a specific input is determined by counting its fundamental operations. For sorting, these are comparisons (A[j] > A[j+1]) and data movements (swap or shift).

    Searching, Sorting, Selection and Hashing: Solved Questions with Step-by-Step Explanations (10 Problems)

    Question 1 · Algorithms · 2026_Set2 NAT
    Consider an array . Suppose the merge sort algorithm is executed on array to sort it in increasing order. The merge sort algorithm will carry out a total of 7 merge operations.

    A merge operation on sorted left array and sorted right array is said to be void if the output of the merge operation is the elements of array followed by the elements of array .

    The number of void merge operations among these 7 merge operations is __________. (answer in integer)
    Correct Answer:

    3

    Step-by-Step Solution

    Key idea: This is a merge sort operation counting question, recognisable because it asks for the number of "void" merge operations on a specific array. A void merge occurs when the left subarray's maximum element is less than or equal to the right subarray's minimum element.

    Step 1: Trace the merge sort tree for .

    Step 2: Level 3 (size 1 to 2):

    • Merge and . Not void ().
    • Merge and . Void (). (Count = 1)
    • Merge and . Not void ().
    • Merge and . Void (). (Count = 2)

    Step 3: Level 2 (size 2 to 4):

    • Merge and . Not void ().
    • Merge and . Not void ().

    Step 4: Level 1 (size 4 to 8):

    • Merge and . Void (). (Count = 3)

    Step 5: Total void merges = 3.

    Answer: 3

    Question 2 · Algorithms · 2026_Set2 MCQ
    Consider an array of integers of size . The indices of run from to . An algorithm is to be designed to check whether satisfies the condition given below.


    Which one of the following gives the worst case time complexity of the fastest algorithm that can be designed for the problem?
    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: The condition implies that the sequence of differences between adjacent elements is strictly increasing. This can be verified in a single pass.

    Step 1: Decode the mathematical condition.

    • The condition is: such that , .
    • Let be the difference between adjacent elements.
    • The condition states: If , then .
    • This means the sequence of differences must be strictly increasing.
    • In other words, .

    Step 2: Design the algorithm.

    • We need to check if the array is strictly sorted in ascending order.
    • We can compute on the fly.
    • Iterate from to (indices for ).
    • Check if .
    • Substitute back: Check if .

    Step 3: Analyze complexity.

    • We iterate through the array once from to .
    • Each step involves constant time arithmetic and comparison.
    • Total time: .

    Step 4: Verify lower bound.

    • We must inspect every element at least once to ensure the property holds globally. Thus, is the lower bound.
    • Since we have an algorithm, the tight bound is .

    Answer: A

    Question 3 · Algorithms · 2025_Set2 MCQ
    Consider an unordered list of distinct integers.

    What is the minimum number of element comparisons required to find an integer in the list that is NOT the largest in the list?
    1. A.

      1

    2. B.

    3. C.

    4. D.

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: To find an element that is NOT the largest, we only need to ensure we pick an element that lost at least one comparison. We can do this with just 1 comparison.

    Step 1: Analyze the requirement. We need to output any integer from the list that is not the maximum. We do not need to find the minimum, nor the second largest, nor sort the list.

    Step 2: Consider the smallest case. Let . The elements are and .

    • Compare and .
    • If , then is not the largest. Output .
    • If , then is not the largest. Output .
    • In either case, 1 comparison is sufficient to identify a non-maximum element.

    Step 3: Generalize to .

    • Pick any two elements, say and .
    • Compare them.
    • The smaller of the two is definitely not the largest element in the entire list (because the other one is larger than it, so the smaller one cannot be the global maximum).
    • Thus, we have found an element that is not the largest.
    • Total comparisons: 1.

    Step 4: Verify minimality.

    • Can we do it in 0 comparisons? No, because without comparing, we don't know the relative order, and any element we pick could potentially be the largest.
    • Therefore, 1 is the minimum.

    Answer: A

    Question 4 · Algorithms · 2025_Set2 MCQ
    An array of length with distinct elements is said to be bitonic if there is an index such that is sorted in the non-decreasing order and is sorted in the non-increasing order.

    Which ONE of the following represents the best possible asymptotic bound for the worst-case number of comparisons by an algorithm that searches for an element in a bitonic array ?
    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    D

    Step-by-Step Solution

    Key idea: A bitonic array consists of a sorted ascending part followed by a sorted descending part. We can search in by first finding the peak and then performing binary search on both halves.

    Step 1: Understand the structure. A bitonic array increases to a peak element and then decreases. Let the peak index be .

    • Subarray is sorted in non-decreasing order.
    • Subarray is sorted in non-increasing order.

    Step 2: Find the peak. We can find the peak element in time using a modified binary search.

    • Compare with .
    • If , the peak is in the right half.
    • If , the peak is in the left half (including mid).
    • This takes comparisons.

    Step 3: Search for the element. Once the peak is found:

    • Perform standard binary search on (ascending). This takes .
    • Perform modified binary search on (descending). This also takes .

    Step 4: Total complexity.

    • Finding peak: .
    • Searching left part: .
    • Searching right part: .
    • Total: .

    Step 5: Compare with options.

    • is linear search (too slow).
    • is impossible for unsorted/search problems without preprocessing.
    • is slower than necessary.
    • is the tight bound.

    Answer: D

    Question 5 · Algorithms · 2025_Set1 NAT
    The pseudocode of a function fun() is given below:

    fun(int A[0,…,n-1]){
    for i=0 to n-2
    for j=0 to n-i-2
    if (A[j]>A[j+1])
    then swap A[j] and A[j+1]

    }

    Let be an array storing 30 distinct integers in descending order. The number of swap operations that will be performed, if the function fun() is called with as argument, is __________. (Answer in integer)
    Correct Answer:

    435

    Step-by-Step Solution

    Key idea: This is a sorting algorithm operation counting question, recognisable because it provides pseudocode and asks for the number of swap operations. The added layer is identifying the algorithm and its behavior on a specific input (reverse sorted).

    Step 1: Analyze the pseudocode. The outer loop runs for from to . The inner loop runs for from to . Inside, it swaps and if .

    Step 2: Recognize this as the standard Bubble Sort algorithm.

    Step 3: The input array has distinct integers in descending order. This is the worst-case input for Bubble Sort.

    Step 4: In the worst case, every comparison evaluates to true, so a swap is performed every time.

    Step 5: Calculate the total number of comparisons (and thus swaps).

    For , goes from to (which is iterations).

    For , goes from to (which is iterations).

    ...

    For , goes from to (which is iteration).

    Step 6: Total swaps = .

    Step 7: Substitute : Total swaps = .

    Answer: 435

    Question 6 · Algorithms · 2024_Set1 MCQ

    Given an integer array of size , we want to check if the array is sorted (in either ascending or descending order). An algorithm solves this problem by making a single pass through the array and comparing each element of the array only with its adjacent elements. The worst-case time complexity of this algorithm is

    1. A.

      both and

    2. B.

      but not

    3. C.

      but not

    4. D.

      neither nor

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: This is a linear-time array property verification question, recognisable because it asks for the worst-case time complexity of an algorithm that checks a global property (sorted order) using a single pass and adjacent comparisons.

    Step 1: Analyze the algorithm's description. It makes a "single pass through the array".

    Step 2: In a single pass, the algorithm compares each element with its adjacent element. For an array of size , there are exactly adjacent pairs.

    Step 3: Therefore, the algorithm performs exactly comparisons, regardless of the input array's content.

    Step 4: A function that performs exactly operations (where and are constants) has a time complexity of .

    Step 5: By definition, means the complexity is bounded both above and below by . Thus, it is both (upper bound) and (lower bound).

    Answer: A

    Question 7 · Algorithms · 2023 MCQ
    An algorithm has to store several keys generated by an adversary in a hash table. The adversary is malicious who tries to maximize the number of collisions. Let be the number of keys, be the number of slots in the hash table, and .

    Which one of the following is the best hashing strategy to counteract the adversary?
    1. A.

      Division method, i.e., use the hash function .

    2. B.

      Multiplication method, i.e., use the hash function , where is a carefully chosen constant.

    3. C.

      Universal hashing method.

    4. D.

      If is a prime number, use Division method. Otherwise, use Multiplication method.

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: Adversarial inputs defeat deterministic hashing. Universal hashing uses randomization to prevent the adversary from predicting collisions.

    Step 1: Analyze the threat. The adversary knows the hash function and tries to maximize collisions. If the hash function is fixed (like Division or Multiplication method with fixed constants), the adversary can simply choose keys that all map to the same slot. For example, if , the adversary picks . This results in worst-case time for operations.

    Step 2: Evaluate Division Method. It is deterministic. Once is known, the adversary can easily construct a worst-case input. Thus, it is not robust against a malicious adversary.

    Step 3: Evaluate Multiplication Method. It depends on a constant . If is fixed and known, the adversary can still analyze the function and find collisions. While it distributes keys better for random data, it does not provide theoretical guarantees against an adversary who knows .

    Step 4: Evaluate Universal Hashing. In universal hashing, we select a hash function randomly from a family at runtime. The adversary does not know which specific was chosen. By definition of a universal family, for any two distinct keys and , the probability of collision . This ensures that the expected number of collisions remains low, regardless of the adversary's strategy.

    Step 5: Conclusion. Universal hashing is the only strategy among the options that provides probabilistic guarantees against an adversary by introducing randomness unknown to the attacker.

    Answer: C

    Question 8 · Algorithms · 2021_Set2 MCQ

    What is the worst-case number of arithmetic operations performed by recursive binary search on a sorted array of size ?

    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: Recursive binary search divides the problem size by 2 in each step. The depth of the recursion tree determines the number of operations.

    Step 1: Analyze the algorithm.

    • Binary search on a sorted array of size .
    • In each recursive call, we calculate the middle index and compare the target with the middle element.
    • Based on the comparison, we recurse on either the left half or the right half.
    • The size of the problem reduces from to .

    Step 2: Formulate the recurrence.

    • Let be the number of arithmetic operations/comparisons.
    • .
    • The term accounts for calculating mid and the comparison.

    Step 3: Solve the recurrence.

    • This is a standard recurrence solved by the Master Theorem or iteration.
    • Depth of recursion = .
    • At each level, constant work is done.
    • Total operations .

    Step 4: Determine the asymptotic bound.

    • Worst-case occurs when the element is not present or is at a leaf.
    • Number of steps = .
    • This is .

    Step 5: Match with options.

    • Option B is .

    Answer: B

    Question 9 · Algorithms · 2021_Set1 MCQ
    Consider the following array.

    2332456972738997

    Which algorithm out of the following options uses the least number of comparisons (among the array elements) to sort the above array in ascending order?
    1. A.

      Selection sort

    2. B.

      Mergesort

    3. C.

      Insertion sort

    4. D.

      Quicksort using the last element as pivot

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is a sorting algorithm operation counting question, recognisable because it provides a specific array and asks which algorithm uses the least number of comparisons. The added layer is recognizing the initial state of the array.

    Step 1: Observe the given array: .

    Step 2: Notice that the array is already sorted in ascending order. Let .

    Step 3: Evaluate each option for an already sorted array:

    • Selection sort: Always performs comparisons regardless of input order. For , this is comparisons.
    • Mergesort: Always performs comparisons. For , it is around comparisons (specifically, 17 to 21 depending on implementation).
    • Insertion sort: For an already sorted array, the inner loop condition fails immediately on the first check for each element. It performs exactly comparisons. For , this is comparisons.
    • Quicksort (last element as pivot): For an already sorted array, this is the worst-case scenario. It performs comparisons. For , this is 28 comparisons.

    Step 4: Compare the counts: (Insertion) is the least.

    Answer: C

    Question 10 · Algorithms · 2021_Set1 MCQ

    Let be an array containing integers. Let be the lowest upper bound on the number of comparisons of the array elements, required to find the minimum and maximum values in an arbitrary array of elements. Which one of the following choices is correct?

    1. A.

    2. B.

      and

    3. C.

      and

    4. D.

      and

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is a comparison bounds question, recognisable because it asks for the lowest upper bound on the number of comparisons required to find both the minimum and maximum values simultaneously.

    Step 1: The naive approach finds the minimum in comparisons and the maximum in another comparisons, totaling comparisons.

    Step 2: The optimal approach processes elements in pairs. Divide the elements into pairs.

    Step 3: Compare the two elements within each pair. This takes comparisons and yields a local minimum and a local maximum for each pair.

    Step 4: Find the global minimum by comparing the local minimums. This takes comparisons.

    Step 5: Find the global maximum by comparing the local maximums. This takes comparisons.

    Step 6: Total comparisons .

    Step 7: This value is strictly less than or equal to . For (with the minor exception of where it equals ), it is greater than . Among the choices, Option C is the only one that correctly bounds the optimal count from above by and generally satisfies the lower bound for meaningful array sizes.

    Answer: C

    More previous year questions (pyqs) in this unit