chapter
    Algorithms PYQs for GATE DA

    GATE DA Algorithms: 6 chapters, 14 previous year questions (45% of Programming, Data Structures and Algorithms), 752 practice questions and one solved questio

    A question from this chapter

    Question 1
    2026 PYQ
    Level 3: Exam Standard
    Let A be a sorted array containing 1000 distinct integers. You perform a recursive binary search on A to find an element y. Suppose each comparison checks whether the middle element computed during the current recursive step is equal to, less than, or greater than y.
    The maximum number of comparisons that may have to be performed if y is not an element of A is _______ . (Answer in integer)
    Question 2
    2026 PYQ
    Level 3: Exam Standard
    Consider the problem of sorting the given array in ascending order:

    P = [1, 2, 3, 5, 4]

    Consider two sorting algorithms Bubble Sort (BS) and Insertion Sort (IS).

    Let N1 be the total number of comparisons done by BS on the elements of P and N2 be the total number of comparisons done by IS on the elements of P.

    Which of the following options is/are correct?
    Question 3
    2026 PYQ
    Level 3: Exam Standard
    Consider that the quick sort algorithm is used to sort an array of n distinct randomly ordered elements. In every call, the pivot is chosen as the first element of the current subarray.
    Let denote the expected time to sort the array. Assume that the time to partition is linear in the size of the current subarray.
    Which of the following recurrence relations correctly represents in this scenario?
    Question 4
    2026 PYQ
    Level 3: Exam Standard
    Consider a directed graph , where is the finite set of vertices and is the set of directed edges between the vertices. may contain cycles but there is no self-loop. Further, may not be strongly connected.

    Let be the graph obtained by reversing the directions of all the edges in without changing the set of vertices.

    Assume that Breadth First Search (BFS) or Depth First Search (DFS) from any given vertex of a graph visits only the reachable vertices from in that graph.

    Which of the following statements must always be true, regardless of the structure of ?
    Question 5
    2024 PYQ
    Level 3: Exam Standard
    Consider the directed acyclic graph (DAG) below:
    PRQSVUT
    Which of the following is/are valid vertex orderings that can be obtained from a
    topological sort of the DAG?
    Question 6
    2024 PYQ
    Level 3: Exam Standard
    Consider the function computeS(X) whose pseudocode is given below:

    computeS(X)

    for to

    if

    end if
    end for
    return S

    Which ONE of the following values is returned by the function computeS(X)
    for X = [6, 3, 5, 4, 10]?
    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.

    Algorithms PYQs for GATE DA

    GATE DA Algorithms: 6 chapters, 14 previous year questions (45% of Programming, Data Structures and Algorithms), 752 practice questions and one solved question from each chapter.

    About Algorithms Previous Year Questions (PYQs)

    14 previous year questions from Algorithms in GATE DA, grouped by chapter with the exam year, answer key and step-by-step solution for each.

    Algorithms Weightage in GATE DA

    Algorithms accounts for 14 of 31 Programming, Data Structures and Algorithms previous year questions in our bank (45%), about 4.7 per paper across 3 papers.

    Algorithms Chapter Matrix

    ChapterTopicsPYQsShare of unit PYQsPractice questions
    Searching Algorithms and Binary SearchBinary Search Recurrence and Worst-Case Comparisons, Binary Search Preconditions and Data Representation321%157
    Elementary Sorting AlgorithmsBubble, Insertion and Selection Sort Pass Analysis, Insertion Sort Swaps and Nearly Sorted Arrays321%160
    Quicksort and Divide-and-Conquer AnalysisQuicksort Partitioning and Swap Count, Expected Recurrence Analysis of Randomized Quicksort214%118
    Graph Traversal: BFS and DFSReachability in Directed Graph Traversals, DFS Discovery Order and Edge Classification, Graph Traversal on Given Diagrams429%210
    Directed Acyclic Graphs and Topological OrderingTopological Ordering of DAGs17%52
    Dynamic Programming and Sequence ProcessingDynamic Programming for Consecutive Sequence Lengths17%55

    More from Programming, Data Structures and Algorithms

    One Solved Question from Each Algorithms Chapter

    Question 1 · Searching Algorithms and Binary Search · 2026 NAT
    Let A be a sorted array containing 1000 distinct integers. You perform a recursive binary search on A to find an element y. Suppose each comparison checks whether the middle element computed during the current recursive step is equal to, less than, or greater than y.
    The maximum number of comparisons that may have to be performed if y is not an element of A is _______ . (Answer in integer)
    Correct Answer:

    10.00

    Step-by-Step Solution

    Insight: A 3-way comparison counts as exactly 1 comparison per step. The max comparisons is simply the depth of the recursion tree.

    Exam route: Use the formula . For , .

    Learning route:

    The problem specifies a 3-way comparison (equal, less, greater). This counts as 1 comparison per recursive step.

    The maximum number of comparisons is the maximum depth of the recursion tree, which occurs when the element is not found or is at the deepest leaf.

    The exact formula for the maximum depth (number of comparisons) is .

    Given :

    .

    .

    Total comparisons = .

    If the problem had specified two separate checks (e.g., if A[mid] == y then else if A[mid] < y), the worst case would be 2 comparisons per step, yielding 20. But the 3-way comparison explicitly avoids this trap.

    Question 2 · Elementary Sorting Algorithms · 2026 MSQ
    Consider the problem of sorting the given array in ascending order:

    P = [1, 2, 3, 5, 4]

    Consider two sorting algorithms Bubble Sort (BS) and Insertion Sort (IS).

    Let N1 be the total number of comparisons done by BS on the elements of P and N2 be the total number of comparisons done by IS on the elements of P.

    Which of the following options is/are correct?
    1. A.

      N1 = 10, N2 = 4

    2. B.

      N1 > N2

    3. C.

      IS on P will perform only one swap

    4. D.

      Both BS and IS on P will make at least one unnecessary comparison (i.e., comparing elements that are already in correct order)

    Correct Answer:

    ["B","C","D"]

    Step-by-Step Solution

    Insight: This is a pass-analysis question recognisable because it asks for exact operation counts on a tiny concrete array. The pivot is that Bubble Sort (standard, no early termination) has a deterministic comparison count while Insertion Sort's count is data-dependent and must be traced element by element.

    Exam route:

    1. Compute N1 using the fixed formula: for , standard Bubble Sort does ... wait, that is only for a full sort. Let us recompute carefully: pass 1 does , pass 2 does , pass 3 does , pass 4 does . Total .
    2. Trace Insertion Sort on :
    • key=2: compare with 1 (1 cmp), no shift.
    • key=3: compare with 2 (1 cmp), no shift.
    • key=5: compare with 3 (1 cmp), no shift.
    • key=4: compare with 5 (shift), compare with 3 (stop) → 2 cmp, 1 shift.
    • Total , shifts .
    1. Evaluate options: ; holds; IS does exactly 1 swap; BS in passes 2–4 compares already-sorted adjacent pairs (unnecessary), and IS compares 4 with 5 which are out of order but the comparison that stops at 3 (already in correct relative order with 4) is the boundary check — BS clearly makes unnecessary comparisons, so D holds.

    Learning route:

    Bubble Sort (standard, no flag) always performs exactly comparisons regardless of input. For , .

    Insertion Sort comparisons depend on how far each key travels. Tracing :

    • key=2 vs {1}: 1 comparison, 0 shifts.
    • key=3 vs {1,2}: 1 comparison, 0 shifts.
    • key=5 vs {1,2,3}: 1 comparison, 0 shifts.
    • key=4 vs {1,2,3,5}: compares with 5 (shift), then with 3 (stop) → 2 comparisons, 1 shift.

    Total , shifts = 1.

    Option A claims — wrong because , not 4.

    Option B: — true.

    Option C: IS performs exactly 1 swap/shift — true.

    Option D: BS in passes 2, 3, 4 compares pairs already in correct order (e.g. 1 and 2 in pass 2); IS compares 4 with 5 which are out of order but the stop-comparison with 3 is against an element already in correct relative position — both algorithms make at least one such comparison. True.

    Verification: re-running both algorithms on confirms , 1 IS shift, and unnecessary comparisons in both.

    Answer: B, C, D.

    Question 3 · Quicksort and Divide-and-Conquer Analysis · 2026 MCQ
    Consider that the quick sort algorithm is used to sort an array of n distinct randomly ordered elements. In every call, the pivot is chosen as the first element of the current subarray.
    Let denote the expected time to sort the array. Assume that the time to partition is linear in the size of the current subarray.
    Which of the following recurrence relations correctly represents in this scenario?
    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    D

    Step-by-Step Solution

    Key idea: This is an expected recurrence analysis problem for Quicksort, recognizable by the phrases "expected time", "randomly ordered elements", and a fixed pivot position (first element).

    Step 1: Understand the pivot selection dynamics.

    Although the pivot is deterministically chosen as the first element of the subarray, the array itself is randomly ordered. This means the first element is equally likely to be the st, nd, ..., or -th smallest element in the subarray.

    Step 2: Determine the subproblem sizes.

    Let the rank of the chosen pivot be (where ranges from to ).

    • The left subarray will contain the elements smaller than the pivot.
    • The right subarray will contain the remaining elements larger than the pivot.

    Step 3: Formulate the expected time recurrence.

    Since each possible rank occurs with a uniform probability of , the expected time is the average of the expected times of all possible splits, plus the time required for the partitioning step itself.

    Mathematically, this is expressed as:

    Step 4: Match with the given options.

    This derived formula exactly matches the fourth option.

    Answer: D

    Question 4 · Graph Traversal: BFS and DFS · 2026 MCQ
    Consider a directed graph , where is the finite set of vertices and is the set of directed edges between the vertices. may contain cycles but there is no self-loop. Further, may not be strongly connected.

    Let be the graph obtained by reversing the directions of all the edges in without changing the set of vertices.

    Assume that Breadth First Search (BFS) or Depth First Search (DFS) from any given vertex of a graph visits only the reachable vertices from in that graph.

    Which of the following statements must always be true, regardless of the structure of ?
    1. A.

      If is a reachable vertex in the BFS of from , then is also a reachable vertex in the DFS of from .

    2. B.

      In , the BFS traversal from will visit exactly the same set of vertices as the DFS from in .

    3. C.

      The order of vertices visited in the BFS of from is the reverse of the order of vertices visited in the DFS of from .

    4. D.

      If is a reachable vertex in the DFS of from , then is also a reachable vertex in the BFS of from .

    Correct Answer:

    D

    Step-by-Step Solution

    Insight: BFS/DFS on from finds exactly the set of vertices that can reach in . BFS/DFS on from finds exactly the set of vertices reachable from in .

    Exam route: Translate each option into reachability language in .

    Learning route:

    1. Let = vertices reachable from in . This is what DFS/BFS on from returns.
    2. Let = vertices that can reach in . This is what DFS/BFS on from returns.
    3. Option A: . False. Counter-example: only. can reach but cannot reach .
    4. Option B: . False. Same counter-example.
    5. Option C: BFS order is reverse of DFS order. False. BFS is level-by-level, DFS is deep-first; no reason for reversal.
    6. Option D: in in .

    If is reachable from in , there is a path .

    Reversing all edges gives in .

    So is reachable from in . This is exactly in (which is computed by BFS on from ).

    True.

    Answer: D.

    Question 5 · Directed Acyclic Graphs and Topological Ordering · 2024 MSQ
    Consider the directed acyclic graph (DAG) below:
    PRQSVUT
    Which of the following is/are valid vertex orderings that can be obtained from a
    topological sort of the DAG?
    1. A.

      P Q R S T U V

    2. B.

      P R Q V S U T

    3. C.

      P Q R S V U T

    4. D.

      P R Q S V T U

    Correct Answer:

    ["B","D"]

    Step-by-Step Solution

    Insight: A topological sort is valid if and only if for every directed edge , vertex appears before vertex in the linear ordering.

    Exam route: Extract all directed edges from the graph diagram. Check each given option to see if it violates any of these precedence constraints. Eliminate options with violations.

    Learning route:

    Step 1: Identify the vertices and directed edges from the SVG diagram.

    The edges are: , , , , , and .

    Step 2: List the precedence constraints derived from these edges:

    • must appear before .
    • must appear before .
    • must appear before and .
    • must appear before .
    • must appear before .

    Step 3: Evaluate each option against these constraints.

    • Option A ("P Q R S T U V"): appears before . This violates the constraint . Invalid.
    • Option B ("P R Q V S U T"): are before ; is before and ; is before ; is before . All constraints are satisfied. Valid.
    • Option C ("P Q R S V U T"): appears before . This violates the constraint . Invalid.
    • Option D ("P R Q S V T U"): are before ; is before and ; is before ; is before . All constraints are satisfied. Valid.

    Step 4: Conclude that options B and D are the valid topological orderings.

    Question 6 · Dynamic Programming and Sequence Processing · 2024 MCQ
    Consider the function computeS(X) whose pseudocode is given below:

    computeS(X)

    for to

    if

    end if
    end for
    return S

    Which ONE of the following values is returned by the function computeS(X)
    for X = [6, 3, 5, 4, 10]?
    1. A.

      [1, 1, 2, 3, 4]

    2. B.

      [1, 1, 2, 3, 3]

    3. C.

      [1, 1, 2, 1, 2]

    4. D.

      [1, 1, 2, 1, 5]

    Correct Answer:

    C

    Step-by-Step Solution

    Insight: This is a consecutive sequence DP question where S[i] represents the length of the longest non-decreasing contiguous subarray ending at index i. The pseudocode implements the standard "extend or reset" pattern.

    Exam route: Trace the pseudocode step by step for the given array X = [6, 3, 5, 4, 10].

    Learning route:

    • i=1: S[1] = 1 (base case, single element is always valid).
    • i=2: X[1]=6, X[2]=3. Check 6 <= 3: false. So S[2] remains 1 (reset).
    • i=3: X[2]=3, X[3]=5. Check 3 <= 5: true. So S[3] = 1 + S[2] = 1 + 1 = 2 (extend).
    • i=4: X[3]=5, X[4]=4. Check 5 <= 4: false. So S[4] remains 1 (reset).
    • i=5: X[4]=4, X[5]=10. Check 4 <= 10: true. So S[5] = 1 + S[4] = 1 + 1 = 2 (extend).

    Final array: S = [1, 1, 2, 1, 2].

    Verification: The longest non-decreasing contiguous subarrays ending at each index are: [6] (length 1), [3] (length 1), [3, 5] (length 2), [4] (length 1), [4, 10] (length 2). This matches S = [1, 1, 2, 1, 2].