Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-181

Min-Max Heap Level Logic

Problem

Classify the level of a node in a min-max heap and find its grandparent.

A min-max heap alternates min levels and max levels: even depths (0, 2, 4, ...) are min levels and odd depths are max levels. The grandparent of index i is ((i - 1) // 2 - 1) // 2, or -1 when i is in the top two levels.

Return ["min" or "max", grandparent_index].

Hint

The depth of index i in a complete binary tree is floor(log2(i + 1)).

Test cases

case 1
in: [0]
expected: ["min",-1]
case 2
in: [1]
expected: ["max",-1]
case 3
in: [3]
expected: ["min",0]
case 4
in: [8]
expected: ["max",1]
case 5
in: [14]
expected: ["max",2]
Starter codePython
def minmax_level(i):
    # Your code here
    pass

5 test cases · run your code against them in the editor