Code RoomHeap construction swap counts
MediumPrep Room Coding #4926

Heap construction swap counts

CodingCode quality & reviewAlgorithms & data structuresMid–Senior~20 min

A level loader stores enemy spawn priorities in an array and turns that array into a min-heap at load time. Two ways are on the table and the studio wants the swap counts side by side. The first is the bottom-up build: for i from n / 2 - 1 down to 0, sift index i down, where sifting down compares a node with its children, picks the smaller child (the left one when the two children are equal), swaps if that child is strictly smaller than the node, and repeats from the node's new position. The second inserts the values one at a time, left to right, into an initially empty heap, sifting each new value up while it is strictly smaller than its parent. Return two numbers: the swaps the bottom-up build performs, then the swaps the repeated insertion performs.

Implement
heapify_swap_counts(priorities: list[int]) → list[int]
Examples
in[[5,3,8,1,9,2]]out[4,4]
in[[1,2,3,4,5]]out[0,0]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 20 min
InputExpectedGot
[[5,3,8,1,9,2]][4,4]not run yetsample
[[1,2,3,4,5]][0,0]not run yetsample