Skip to content
GATEiit

GATE DA 2024 Q41: What a recursive swap function does to a list

Question

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.

Try it: multiple choice: exactly one correct option

Select one option
Wrong answer costs 2/3 mark in GATE.
Show answer and explanation

Answer: C (official GATE 2024 final answer key)

Concept used: Two-pointer recursion: swap the ends, then move both ends inward.

First step: Each call swaps D[s1] and D[s2] and recurses on (s1+1, s2−1) until the pointers meet.

Full step-by-step solution

Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.

Share this question

WhatsAppDownload image

Source: official GATE 2024 DA question paper and final answer key, organised by IISc Bangalore. Question paper · Answer key