GATE DA 2024 Q41: What a recursive swap function does to a list
- Programming, Data Structures and Algorithms
- 2 marks
- MCQ
- −2/3 if wrong
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
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
Source: official GATE 2024 DA question paper and final answer key, organised by IISc Bangalore. Question paper · Answer key
More Programming, Data Structures and Algorithms PYQs
- 2026 · Q15Expected-time recurrence for randomised-input quicksort
- 2026 · Q16Python mutable default argument output
- 2026 · Q25Binary tree from preorder and inorder traversals
- 2026 · Q31Worst-case comparisons in binary search of 1000 elements
- 2026 · Q39Number of calls made by a Fibonacci-like recursion
- 2026 · Q40Reachability in a graph and its reverse