IAE Academy

GATE 2024 DA Q.41: solution and answer

GATE 2024 · DAQ.41 · 2 marks · 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.

See the question in the paper

Answer and solution

ANSWER

The function reverses the list D in place between indices s1 and s2, both inclusive, which is option (C).

Official answer: CSource: Official answer key, GATE 2024 DA (IISc Bengaluru)

  1. The function is:

    def fun(D, s1, s2):
            if s1 < s2:
                D[s1], D[s2] = D[s2], D[s1]
                fun(D, s1+1, s2-1)

    Both the swap and the recursive call sit inside the if, so when s1 < s2 is false the function does nothing and returns. This is the base case, reached when the indices meet (s1 == s2) or cross (s1 > s2).

  2. When s1 < s2, the tuple assignment D[s1], D[s2] = D[s2], D[s1] evaluates the right-hand side first and then assigns, so it exchanges the two end elements of the window with no temporary variable needed.

  3. The call fun(D, s1+1, s2-1) moves both indices one step inward. It shrinks the window by two and swaps the next pair counted from the outside. The list D is mutable and passed by reference, so every swap changes the caller's list.

  4. Trace with D = [a, b, c, d, e], s1 = 0, s2 = 4. The pair (0, 4) gives [e, b, c, d, a]. The pair (1, 3) gives [e, d, c, b, a]. At (2, 2) the test 2 < 2 fails, so the call stops. The result is the reverse of the list.

  5. Even length, D = [a, b, c, d] with s1 = 0, s2 = 3. The pair (0, 3) gives [d, b, c, a]. The pair (1, 2) gives [d, c, b, a]. At (2, 1) the test 2 < 1 fails and the call stops. This is again the full reverse.

  6. General argument by induction on the window length . For nothing happens, and a window of length 0 or 1 is its own reverse. For the first swap puts the old last element at index s1 and the old first element at index s2. The recursion then reverses the inner window s1+1 .. s2-1, which has length , by the induction hypothesis. Together this gives the reverse of the whole window s1 .. s2. Elements outside the window are never touched.

  7. Nothing in the code compares elements, so it cannot find a minimum (A) or sort (B). It performs about swaps rather than a single swap, so (D) is wrong, since (D) would leave the interior unchanged. Only option (C) matches. The answer is C.

KEY IDEA

A recursive two-pointer swap: swap the ends of the window, then recurse on the window shrunk by one at each end, until the pointers meet or cross. That is in-place reversal of the segment.

WATCH OUT FOR

Reading the single swap D[s1], D[s2] = D[s2], D[s1] and picking (D) without noticing the recursive call repeats the swap on the inner pairs. Another error is thinking swaps imply sorting. No comparison between elements ever happens, so the result depends only on positions, never on values.

From the GATE 2024 DA paper. Every question of this paper · Download the full paper · All 10 Python questions, with notes

More Python questions