DSA interview question
A DNA-sequence tool stores bases in a singly linked list and must check whether a sequence reads the same forwards and backwards. With an array you'd compare from both ends, but a singly linked list can't walk backwards. Copying the values to an array is O(n) extra space. The O(1)-space technique combines three earlier building blocks: find the middle with fast and slow pointers, reverse the second half in place, compare the two halves node by node, and then restore the list so the caller's data isn't left scrambled.
Example: 1 -> 2 -> 2 -> 1 → true; 1 -> 2 → false.
bool isPalindrome(Node* head); // O(1) extra space; the list is restored before returning
| Input list | Output | Explanation |
|---|---|---|
1 -> 2 -> 2 -> 1 | true | Even length |
1 -> 2 -> 3 -> 2 -> 1 | true | Odd length: the middle is ignored |
1 -> 2 | false | |
1 -> 2 -> 3 | false | |
1 | true | |
(empty) | true |
(Confirm: compare values; O(1) extra space is expected; the list must be unchanged after the check.)
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