Insert into a Min-Heap
Updated
Problem statement
Given an array heap that represents a valid binary min-heap in level order and an integer value, insert value and return the resulting level-order array.
Append the value at the end, then repeatedly swap it with its parent while it is strictly smaller than that parent. Equal values do not swap.
Function
insertIntoMinHeap(heap: int[], value: int) → int[]Examples
Example 1
heap = [1,3,6,5,9,8]value = 2return = [1,3,2,5,9,8,6]After appending 2, swap it with its parent 6. Its new parent is 1, so the process stops.
Example 2
heap = []value = 4return = [4]The inserted value becomes the root of the previously empty heap.
Example 3
heap = [1,2,2,7]value = 2return = [1,2,2,7,2]The appended value equals its parent, so no swap occurs.
Constraints
0 <= heap.length <= 200000.heapis a valid binary min-heap.- Every stored value and
valueis a 32-bit signed integer.
More Goldman Sachs Onsite Interview questions
- Best Average Student ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Knight Probability in ChessboardONSITE INTERVIEW · Seen Nov 2026
- Meeting RoomsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Meeting Rooms Without a Sweep LineONSITE INTERVIEW · Seen Oct 2026
- First Non-Repeating CharacterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Root of the Largest TreePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Dungeon HealthONSITE INTERVIEW · Seen Sep 2026
- Insert, Delete, And Get Random In Constant TimeONSITE INTERVIEW · Seen Sep 2026
- Least Recently Used CacheONSITE INTERVIEW · Seen Sep 2026
- Staircase Search In A Sorted MatrixONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Meeting Rooms III: Most Booked RoomGoogle · ONSITE INTERVIEWGoogle interview questions
- Assign Partitions to the Least-Loaded ServersAmazon · ONSITE INTERVIEW
- Cursor-Based Pagination Over Sorted LogsRoblox · ONSITE INTERVIEWRoblox interview questions
- Issue and Reuse 16-Bit TokensMeta · ONSITE INTERVIEWMeta interview questions
- Order Matching on ExecuteSquarepoint Capital · ONSITE INTERVIEWSquarepoint Capital interview questions
- Weighted Server Load Balancer with TTLStripe · ONSITE INTERVIEWStripe interview questions
- Apply Binary State OperationsZipRecruiter · OAZipRecruiter interview questions
- Shortest-Job-First Process SchedulingByteDance · PHONE SCREENByteDance interview questions
- Dynamic Batch Completion TimesAmbience Healthcare · PHONE SCREEN
- Automation Pipeline, Part 3: Stage DurationPlaid · PHONE SCREENPlaid interview questions