PPractick

DSA interview question

Reverse an Array Using Recursion

Easy · Algorithms · Topics: recursion, array, two pointer

The problem

A playlist app has a "reverse order" button, and the exercise is to implement the reversal in place using recursion rather than a loop. The classic approach uses two pointers: swap the first and last elements, then recursively reverse the part in between. It's a clean illustration of how recursion shrinks the problem from both ends, and the same structure reappears in recursive palindrome checks and in divide-and-conquer algorithms.

Example: [1, 2, 3, 4, 5] → [5, 4, 3, 2, 1].

Function signature

void reverseTwoPointers(vector<int>& a, int left, int right);  // call with (a, 0, n - 1)
void reverseOnePointer(vector<int>& a, int i = 0);             // swaps a[i] and a[n - 1 - i]

Test cases

Input aOutputExplanation
[1, 2, 3, 4, 5][5, 4, 3, 2, 1]The middle element stays put
[1, 2, 3, 4][4, 3, 2, 1]Even length: every element moves
[1, 2][2, 1]One swap
[7][7]Nothing to swap
[][]Empty array

(Confirm: reverse in place; the original vector is modified.)

How interviewers phrase it

Clarifying questions to ask

  1. In place, or return a new array?
  2. Is recursion required?
  3. How large can the array be? (Recursion depth is about n / 2.)

Follow-ups to expect

  1. "What's the base case?"
  2. "Can you do it with a single index?"
  3. "Recursion depth?"
  4. "How would you reverse only part of the array, say indices l to r?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in