Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-164

Disk Block Bitmap Allocation

Problem

Simulate disk block allocation with a bitmap and first-fit search.

num_blocks blocks start free. operations contains "allocate" (values[i] is the number of contiguous blocks wanted, first fit, returning the start index or -1), "free" (values[i] is [start, count], returning None), or "bitmap" (records the current bitmap).

Return the list of recorded results.

Hint

Track the current run of free blocks and reset it at every allocated block.

Test cases

case 1
in: [8,["allocate","allocate","free","allocate","bitmap"],[3,4,[0,3],3,0]]
expected: [0,3,null,0,[1,1,1,1,1,1,1,0]]
case 2
in: [4,["allocate","allocate"],[2,3]]
expected: [0,-1]
case 3
in: [4,["allocate","free","allocate"],[4,[1,2],3]]
expected: [0,null,-1]
case 4
in: [6,["allocate","allocate","free","allocate","bitmap"],[2,2,[0,2],2,0]]
expected: [0,2,null,0,[1,1,1,1,0,0]]
Starter codePython
def bitmap_allocate(num_blocks, operations, values):
    # Your code here
    pass

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