DSA interview question
A game server stores player scores in a min-heap (to evict the lowest score quickly), but a new leaderboard feature needs a max-heap of the same scores, built in place and fast. The min-heap order doesn't help, so treat the array as arbitrary and run bottom-up heapify for a max-heap: sift down every internal node from the last one to the root. That costs O(n), not O(n log n) as n separate insertions would.
Example: [3, 5, 9, 6, 8, 20, 10, 12, 18, 9] → [20, 18, 10, 12, 9, 9, 3, 5, 6, 8].
void convertMinToMaxHeap(vector<int>& a); // in place
| Input (a min-heap) | Output (a max-heap) | Explanation |
|---|---|---|
[3, 5, 9, 6, 8, 20, 10, 12, 18, 9] | [20, 18, 10, 12, 9, 9, 3, 5, 6, 8] | |
[1, 2, 3] | [3, 2, 1] | |
[1, 2, 3, 4] | [4, 2, 3, 1] | |
[5, 5] | [5, 5] | |
[10] | [10] |
(Confirm: any valid max-heap arrangement is acceptable; the outputs shown are what bottom-up heapify produces.)
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