PPractick

DSA interview question

Heap Fundamentals: Heap Sort In Place

Easy · Data structures · Topics: heap, theory

The problem

An embedded device must sort sensor readings in place with guaranteed O(n log n) time and O(1) extra memory. Quicksort's worst case and merge sort's buffer are both unacceptable. Heap sort meets all three, and building it exercises every heap fundamental:

Example: [5, 2, 9, 1, 5, 6] → [1, 2, 5, 5, 6, 9].

Function signature

void heapSort(vector<int>& a);  // ascending, in place

Test cases

InputOutputExplanation
[5, 2, 9, 1, 5, 6][1, 2, 5, 5, 6, 9]Duplicates are fine
[3, 2, 1][1, 2, 3]
[-1, -3, 2, 0][-3, -1, 0, 2]
[7][7]
[][]

(Confirm: in-place, O(1) extra memory; stability isn't required, and heap sort isn't stable.)

How interviewers phrase it

Clarifying questions to ask

  1. Ascending or descending? (Ascending uses a max-heap.)
  2. Is O(1) extra memory required? (Yes, which is the point of heap sort.)
  3. Does stability matter? (Heap sort isn't stable.)

Follow-ups to expect

  1. "How is a heap stored in an array?"
  2. "What's the heap property?"
  3. "Why is building a heap O(n), not O(n log n)?"
  4. "sift-up vs sift-down?"
  5. "Complexity of heap sort?"

Practise questions like this with a real engineer

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 interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in