LearnThatStack Ace your next interview
Trees, BSTs & Heaps · question
Question 10 of 76

What is a binary heap, and what shape and ordering properties define it?

beginner
← All Trees, BSTs & Heaps questions
Re-explain
Visual ↓

A binary heap is a complete binary tree with a simple ordering between parents and children. Two properties define it, and both must always hold.

The shape property says the tree is complete. Every level is full except possibly the last, which fills left to right. That compact shape lets it live in a plain array with no pointers.

The heap property is a parent-child rule. In a max-heap, every parent is at least as large as its children, so the largest value sits at the root. A min-heap flips it, keeping the smallest at the root. The rule only relates parents to their own children, not siblings, so a heap is far weaker than a sorted order. That weakness makes reading the top O(1) while keeping inserts cheap.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Trees, BSTs & Heaps One question at a time. Answer out loud, get graded, see what you missed.
See it - interactive diagram
A valid max-heap that is not sorted
  • 90In a max-heap, the largest value is the root.
    • 80It is at least as large as both its children.
      • 70It only has to be no larger than its parent, 80.
      • 50No rule orders it against its sibling, 70.
    • 30It is smaller than 70, which sits a level deeper.
      • 20The last level fills from the left, with no gaps.

Both heap rules hold, yet 70 sits deeper than the smaller 30. A heap only compares a parent with its own children, so it is not sorted.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

What you can say

  1. A binary heap is a complete binary tree where each parent is ordered against its own children, and two properties have to hold at all times.
  2. The shape property says the tree is complete, so every level is full except maybe the last, and the last level fills from left to right.
  3. Because the shape has no gaps, the whole tree fits in a plain array with no parent or child pointers.
  4. The heap property says that in a max-heap every parent is at least as large as its children, so the biggest value sits at the root, and a min-heap flips that to keep the smallest on top.
  5. That rule only compares a parent with its own children, never siblings, so a heap is much weaker than a sorted order.
  6. That weakness is the trade-off. Reading the top is O(1) and inserts stay cheap, but you don't get sorted order or a fast lookup of any other value.

Weak answers to avoid

  • Names the ordering rule but skips completeness Completeness is half the definition. It's what lets the heap live in an array and keeps the height near log n. Name both properties.
  • Calls a heap sorted or treats it like a BST Order only runs from parent to child. Siblings and left versus right have no rule, so an in-order walk isn't sorted. Say it's weaker than sorted, and that's why it's cheap.
  • Says complete but describes a full or perfect tree Complete means every level is full except the last, which fills left to right. A perfect tree needs every level full, which only some sizes allow.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Trees, BSTs & Heaps cheatsheet.

← Back to all Trees, BSTs & Heaps questions
Pro · $10/mo

65 of 76 Trees, BSTs & Heaps answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime