DSA interview question
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].
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]
Input a | Output | Explanation |
|---|---|---|
[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.)
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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in