DSA interview question
A contacts app stores recently called numbers in a singly linked list, most recent first. When a call comes in, the app checks whether the number is already in the list and where, so it can move that entry to the front. There's no index and no ordering to exploit, so the only option is a linear scan from the head, counting positions until the value turns up or the list ends. It's the linked-list counterpart of linear search.
Example: 1 -> 2 -> 3 -> 4, search 3 → position 3.
int searchPosition(Node* head, int key); // 1-indexed position of the first match, or -1
Input (list, key) | Output | Explanation |
|---|---|---|
(1 -> 2 -> 3 -> 4, 3) | 3 | |
(1 -> 2 -> 3 -> 4, 5) | -1 | Not present |
(empty, 1) | -1 | |
(7 -> 7, 7) | 1 | The first occurrence |
(4 -> -2 -> 9, 9) | 3 | At the tail |
(Confirm: positions are 1-indexed; return the first occurrence, or -1 if the key is absent.)
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