Find all values less than x in an array-based min-heap by pruning subtrees

Quick Overview

Given a min-heap stored as an array and a threshold x, return every value strictly below x in ascending order. The problem tests the array layout of a binary heap, how the heap property lets a traversal skip whole subtrees, and the cost of producing sorted output.

Find all values less than x in an array-based min-heap by pruning subtrees

Company: Stripe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a min-heap represented as an array `heap` and a target value `x`, return all nodes in the heap with a value strictly less than `x`. Use the min-heap property to prune branches: if a node's value is greater than or equal to `x`, none of its descendants can have a value less than `x`, so that entire subtree can be skipped. ### Function Signature ```python def filter_heap_nodes(heap: list[int], x: int) -> list[int]: ``` ### Rules - `heap` uses the standard array layout of a binary heap. The root is `heap[0]`, and the children of `heap[i]` are `heap[2*i + 1]` and `heap[2*i + 2]` when those indices are inside the array. Every parent is less than or equal to each of its children. - Return the qualifying values sorted in ascending order. - Each node counts on its own. A value stored in several nodes below `x` appears in the output once per node. - If no node is less than `x`, return an empty list. ### Constraints - `1 <= heap.length <= 10^5` - `-10^9 <= heap[i] <= 10^9` - `-10^9 <= x <= 10^9` - `heap` is a valid min-heap. ### Examples ```text Input: heap = [1,3,5,7,9,8,10], x = 6 Output: [1,3,5] ``` The root 1 and its children 3 and 5 are below 6. All four grandchildren (7, 9, 8 and 10) are at least 6, so they are left out. ```text Input: heap = [2,4,6,8,10,12,14], x = 10 Output: [2,4,6,8] ``` The values 2, 4, 6 and 8 are below 10. The 10 is excluded because the comparison is strict, and 12 and 14 are larger. ```text Input: heap = [1,4,2,4,9,3,7,5,6,10], x = 5 Output: [1,2,3,4,4] ``` The values below 5 are 1, 4, 2, 4 and 3, at indices 0, 1, 2, 3 and 5. Both 4s are kept and the result is sorted. The node 9 at index 4 is at least 5, so its child 10 at index 9 never needs to be checked.

Overview: Given a min-heap stored as an array and a threshold x, return every value strictly below x in ascending order. The problem tests the array layout of a binary heap, how the heap property lets a traversal skip whole subtrees, and the cost of producing sorted output.

Stripe logo
Stripe
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a min-heap represented as an array heap and a target value x, return all nodes in the heap with a value strictly less than x.

Use the min-heap property to prune branches: if a node's value is greater than or equal to x, none of its descendants can have a value less than x, so that entire subtree can be skipped.

Function Signature

def filter_heap_nodes(heap: list[int], x: int) -> list[int]:

Rules

  • heap uses the standard array layout of a binary heap. The root is heap[0] , and the children of heap[i] are heap[2*i + 1] and heap[2*i + 2] when those indices are inside the array. Every parent is less than or equal to each of its children.
  • Return the qualifying values sorted in ascending order.
  • Each node counts on its own. A value stored in several nodes below x appears in the output once per node.
  • If no node is less than x , return an empty list.

Constraints

  • 1 <= heap.length <= 10^5
  • -10^9 <= heap[i] <= 10^9
  • -10^9 <= x <= 10^9
  • heap is a valid min-heap.

Examples

Input: heap = [1,3,5,7,9,8,10], x = 6
Output: [1,3,5]

The root 1 and its children 3 and 5 are below 6. All four grandchildren (7, 9, 8 and 10) are at least 6, so they are left out.

Input: heap = [2,4,6,8,10,12,14], x = 10
Output: [2,4,6,8]

The values 2, 4, 6 and 8 are below 10. The 10 is excluded because the comparison is strict, and 12 and 14 are larger.

Input: heap = [1,4,2,4,9,3,7,5,6,10], x = 5
Output: [1,2,3,4,4]

The values below 5 are 1, 4, 2, 4 and 3, at indices 0, 1, 2, 3 and 5. Both 4s are kept and the result is sorted. The node 9 at index 4 is at least 5, so its child 10 at index 9 never needs to be checked.