DSA interview question
A search engine gathers results from k shards, and each shard returns its hits as a sorted linked list by score. The front end needs one sorted list. Merging the lists one after another costs O(N · k) for N total nodes. A min-heap of the k current list heads always holds the next smallest node: pop it, append it to the output, and push its successor. That's O(N log k).
Example: [[1,4,5], [1,3,4], [2,6]] → [1,1,2,3,4,4,5,6].
Node* mergeKLists(vector<Node*>& lists); // each list sorted ascending; some may be empty
| Input | Output | Explanation |
|---|---|---|
[[1,4,5], [1,3,4], [2,6]] | [1, 1, 2, 3, 4, 4, 5, 6] | |
[] | [] | No lists |
[[]] | [] | One empty list |
[[2], [1]] | [1, 2] | |
[[1, 2, 3]] | [1, 2, 3] |
(Confirm: the lists are sorted ascending; reuse the existing nodes; N total nodes across k lists.)
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