DSA interview question
A printing queue interleaves jobs from two departments: positions 1, 3, 5, ... belong to Finance and positions 2, 4, 6, ... to Marketing. The manager wants all of Finance's jobs first, then all of Marketing's, each group keeping its original relative order, and the reshuffle must happen in place with O(1) extra memory. Two pointers do it by threading two chains, one through the odd-position nodes and one through the even-position nodes, and then joining the end of the odd chain to the start of the even chain.
Example: 1 -> 2 -> 3 -> 4 -> 5 → 1 -> 3 -> 5 -> 2 -> 4.
Node* oddEvenList(Node* head); // odd positions (1st, 3rd, ...) first, then even positions
| Input list | Output list | Explanation |
|---|---|---|
1 -> 2 -> 3 -> 4 -> 5 | 1 -> 3 -> 5 -> 2 -> 4 | |
2 -> 1 -> 3 -> 5 -> 6 -> 4 -> 7 | 2 -> 3 -> 6 -> 7 -> 1 -> 5 -> 4 | Positions, not values, decide the group |
1 -> 2 | 1 -> 2 | Already grouped |
1 | 1 | |
(empty) | (empty) |
(Confirm: "odd" and "even" refer to 1-indexed positions, not values (LeetCode 328); relative order within each group is preserved.)
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