chapter
    List Processing and In-Place Mutation PYQs for GATE DA

    Solve 2+ List Processing and In-Place Mutation previous year questions for GATE DA 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 PYQ
    Level 3: Exam Standard
    Consider the given Python program.

    def fun(L, i=0):
        if i >= len(L)-1:
            return 0
        if L[i] > L[i+1]:
            L[i+1], L[i] = L[i], L[i+1]
            return 1+fun(L, i+1)
        else:
            return fun(L, i+1)

    data = [5, 3, 4, 1, 2]
    count = 0
    for _ in range(len(data)):
        count += fun(data)
    print(count)

    The output of the program is __________ . (Answer in integer)
    Question 2
    2024 PYQ
    Level 3: Exam Standard
    Consider the following Python function:
    def fun(D, s1, s2):
    if s1 < s2:
    D[s1], D[s2] = D[s2], D[s1]
    fun(D, s1+1, s2-1)
    What does this Python function fun() do? Select the ONE appropriate option
    below.
    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.

    List Processing and In-Place Mutation PYQs for GATE DA

    Solve 2+ List Processing and In-Place Mutation previous year questions for GATE DA with answers and detailed solutions. Free sample questions below.

    Chapter Roadmap: List Processing and In-Place Mutation

    Chapter Roadmap

    Chapter journey

    1. Recursive List Processing and In-Place Mutation
      Current topic. High-importance tracing.
      • Single-index recursive scans
      • Adjacent-swap counting
      • Two-pointer reversal
    2. Chapter mastery
      • Track exact list state after every swap
      • Decide if call is scan or reversal
      • Predict final return value and list
    Weight hint: Core tracing topic. Small code pieces can carry good marks through careful step-by-step execution.

    Topic Hero: Recursion Meets Mutable Lists

    Topic Hero: Recursion Meets Mutable Lists

    A Python list is mutable. If a recursive function changes the list, the changed list is visible to all deeper recursive calls.

    Two core shapes appear repeatedly:

    Shape Indices used Typical operation
    Single-index scan i Compare and possibly swap L[i] and L[i+1]
    Two-pointer reversal s1, s2 Swap D[s1] and D[s2], then move inward
    Key idea: The list is shared, but the index variables are local to each recursive call.

    List Processing and In-Place Mutation: Solved Questions with Step-by-Step Explanations (2 Problems)

    Question 1 · Programming, Data Structures and Algorithms · 2026 NAT
    Consider the given Python program.

    def fun(L, i=0):
        if i >= len(L)-1:
            return 0
        if L[i] > L[i+1]:
            L[i+1], L[i] = L[i], L[i+1]
            return 1+fun(L, i+1)
        else:
            return fun(L, i+1)

    data = [5, 3, 4, 1, 2]
    count = 0
    for _ in range(len(data)):
        count += fun(data)
    print(count)

    The output of the program is __________ . (Answer in integer)
    Correct Answer:

    8.00

    Step-by-Step Solution

    Insight: The inner function fun performs one left-to-right pass of adjacent swaps, returning the number of swaps. The outer loop calls it n times, which is exactly the Bubble Sort algorithm. The total number of swaps in Bubble Sort equals the number of inversions in the initial array.

    Exam route: Count the inversions in [5, 3, 4, 1, 2]. 5 is greater than 3, 4, 1, 2 (4 inversions). 3 is greater than 1, 2 (2 inversions). 4 is greater than 1, 2 (2 inversions). Total = 4 + 2 + 2 = 8.

    Learning route:

    Pass 1: [5, 3, 4, 1, 2] -> 5 bubbles to the end. Swaps: (5,3), (5,4), (5,1), (5,2). Count = 4. Array: [3, 4, 1, 2, 5].

    Pass 2: [3, 4, 1, 2, 5] -> 4 bubbles to index 3. Swaps: (4,1), (4,2). Count = 2. Array: [3, 1, 2, 4, 5].

    Pass 3: [3, 1, 2, 4, 5] -> 3 bubbles to index 2. Swaps: (3,1), (3,2). Count = 2. Array: [1, 2, 3, 4, 5].

    Pass 4 & 5: Already sorted, 0 swaps.

    Total count = 4 + 2 + 2 + 0 + 0 = 8.

    Question 2 · Programming, Data Structures and Algorithms · 2024 MCQ
    Consider the following Python function:
    def fun(D, s1, s2):
    if s1 < s2:
    D[s1], D[s2] = D[s2], D[s1]
    fun(D, s1+1, s2-1)
    What does this Python function fun() do? Select the ONE appropriate option
    below.
    1. A.

      It finds the smallest element in D from index s1 to s2, both inclusive.

    2. B.

      It performs a merge sort in-place on this list D between indices s1 and s2, both inclusive.

    3. C.

      It reverses the list D between indices s1 and s2, both inclusive.

    4. D.

      It swaps the elements in D at indices s1 and s2, and leaves the remaining elements unchanged.

    Correct Answer:

    C

    Step-by-Step Solution

    Insight: The function swaps the elements at s1 and s2, then recursively calls itself with s1+1 and s2-1. This is the standard two-pointer recursive reversal pattern.

    Exam route: Recognize the two pointers moving inwards (s1+1, s2-1) and swapping elements. This reverses the subarray from s1 to s2.

    Learning route:

    Base case: s1 < s2 is false (i.e., s1 >= s2), it stops.

    Recursive step: swaps D[s1] and D[s2], then moves pointers inward.

    This exactly reverses the segment D[s1...s2] in place.

    More previous year questions (pyqs) in this unit