Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-201

Slab Class Sizing

Problem

Find the slab class for an object size in a power-of-two allocator.

Slab classes double starting at 8 bytes. Return the smallest class that can hold an object of the given size, so any size up to 8 maps to 8 and larger sizes round up to the next power of two.

Return the class size in bytes.

Hint

Double the class until it is at least the requested size.

Test cases

case 1
in: [5]
expected: 8
case 2
in: [8]
expected: 8
case 3
in: [17]
expected: 32
case 4
in: [1024]
expected: 1024
case 5
in: [1025]
expected: 2048
Starter codePython
def slab_class(size):
    # Your code here
    pass

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