In interviews, you'll typically use built-in heap libraries. Here's how heaps work internally and how to use them in different languages.- Push (Insert): Add element, bubble up to maintain heap property - O(log n)
- Pop (Extract): Remove root, replace with last element, bubble down - O(log n)
- Peek: Return the root element without removing - O(1)
- Heapify: Convert an array into a heap - O(n)
Python's heapq implements a min-heap by default.Python only has min-heap. For max-heap, negate the values.Java's PriorityQueue is a min-heap by default. Use a comparator for max-heap.JavaScript doesn't have a built-in heap. You can use a simple array with sort, or implement a heap class.Often you need to heap objects by a specific property.- Push: O(log n)
- Pop: O(log n)
- Peek: O(1)
- Heapify: O(n)
- Space: O(n)