Design a disk space manager with first-fit allocation and merging of freed blocks

Quick Overview

Design a disk block manager that allocates contiguous blocks with a first-fit policy, frees ranges of blocks, and reports how many blocks are free. It tests interval bookkeeping, merging adjacent free regions so large requests still fit, and the cost of each operation.

Design a disk space manager with first-fit allocation and merging of freed blocks

Company: NVIDIA

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a disk space manager for a fixed number of contiguous disk blocks numbered from `0` to `totalBlocks - 1`. Initially, all blocks are free. The manager must support allocating contiguous blocks of a requested size using a first-fit strategy: it finds the first (lowest-addressed) contiguous region of free blocks large enough to satisfy the request. If no such region exists, the allocation fails. The manager must also support deallocating a previously allocated range of blocks (marking them free again and merging adjacent free regions), and querying the total number of free blocks. Methods: - `DiskSpaceManager(totalBlocks: int)` creates a disk with `totalBlocks` free blocks. - `allocate(size: int) -> int` allocates `size` contiguous blocks and returns the starting block index of the allocated region, or `-1` if there is not enough contiguous free space. - `deallocate(startBlock: int, size: int) -> None` frees the `size` contiguous blocks starting at `startBlock`. - `freeSpace() -> int` returns the total number of free (unallocated) blocks. Example 1: ```text Input: operations = ["DiskSpaceManager", "allocate", "allocate", "freeSpace", "deallocate", "freeSpace", "allocate", "freeSpace"] arguments = [[10], [3], [4], [], [0, 3], [], [5], []] Output: [null, 0, 3, 3, null, 6, -1, 6] ``` The disk has 10 blocks. `allocate(3)` returns 0 and `allocate(4)` returns 3, leaving blocks 7 to 9 free, so `freeSpace()` is 3. `deallocate(0, 3)` frees blocks 0 to 2 and `freeSpace()` becomes 6. The free regions are now `[0, 3)` and `[7, 10)`, three blocks each, so `allocate(5)` returns `-1` even though 6 blocks are free in total. Free space stays 6. Example 2: ```text Input: operations = ["DiskSpaceManager", "freeSpace", "allocate", "allocate", "deallocate", "allocate", "freeSpace"] arguments = [[5], [], [2], [2], [0, 2], [3], []] Output: [null, 5, 0, 2, null, -1, 3] ``` The disk has 5 blocks, all free. The two `allocate(2)` calls return 0 and 2. After `deallocate(0, 2)` the free regions are `[0, 2)` and `[4, 5)`, so `allocate(3)` returns `-1`. Free space is 3. Example 3: ```text Input: operations = ["DiskSpaceManager", "allocate", "allocate", "allocate", "deallocate", "deallocate", "allocate", "freeSpace"] arguments = [[8], [2], [3], [2], [0, 2], [2, 3], [4], []] Output: [null, 0, 2, 5, null, null, 0, 2] ``` The three allocations return 0, 2 and 5, leaving only block 7 free. `deallocate(0, 2)` frees `[0, 2)`. `deallocate(2, 3)` frees `[2, 5)`, which touches `[0, 2)`, so the two merge into `[0, 5)`. That merged region is what lets `allocate(4)` return 0. Blocks 4 and 7 remain free, so `freeSpace()` is 2. ```hint What to store Marking each block free or used makes every allocation a scan over blocks. Think about a more compact description of the free space, and which ordering makes the first-fit search simple. ``` ```hint Freeing next to free space A freed range can touch free space on its left, on its right, or on both sides. Check what your structure looks like after each case, and whether a later large request can still find room. ``` ### Constraints & Assumptions - `1 <= totalBlocks <= 10^5` - `1 <= size <= totalBlocks` for `allocate` calls - `0 <= startBlock < totalBlocks` and `startBlock + size <= totalBlocks` for `deallocate` calls - At most `10^4` operations in total - Every block in a `deallocate` range is currently allocated. ### Clarifying Questions to Ask - Does a `deallocate` call always match one earlier allocation exactly, or can it free part of an allocation or span two adjacent ones? - What should happen on a double free or on a range that is already partly free: ignore it, report an error, or is it guaranteed not to happen? - How often is `freeSpace` called compared with the other operations, and should it run in constant time? - Is first fit fixed, or should the design make it easy to switch to another placement policy later? ### What a Strong Answer Covers - A representation of the free space that supports a first-fit search, and the order it is kept in. - Splitting a free region on allocation, including the case where the request uses the whole region. - Merging a freed range with free neighbors on either side so that later large requests can fit. - How `freeSpace` is answered and the time and space cost of each operation. - What happens on invalid input such as a double free, or a clearly stated precondition. ### Follow-up Questions - With many small free regions, first fit becomes a linear scan. How would you make `allocate` logarithmic in the number of blocks? - How would you add best-fit allocation, and how would you index the free regions to support it? - How would you detect and reject a double free or a `deallocate` range that overlaps free space? - What changes if callers free by starting block only, without passing the size?

Overview: Design a disk block manager that allocates contiguous blocks with a first-fit policy, frees ranges of blocks, and reports how many blocks are free. It tests interval bookkeeping, merging adjacent free regions so large requests still fit, and the cost of each operation.

NVIDIA logo
NVIDIA
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a disk space manager for a fixed number of contiguous disk blocks numbered from 0 to totalBlocks - 1. Initially, all blocks are free.

The manager must support allocating contiguous blocks of a requested size using a first-fit strategy: it finds the first (lowest-addressed) contiguous region of free blocks large enough to satisfy the request. If no such region exists, the allocation fails.

The manager must also support deallocating a previously allocated range of blocks (marking them free again and merging adjacent free regions), and querying the total number of free blocks.

Methods:

  • DiskSpaceManager(totalBlocks: int) creates a disk with totalBlocks free blocks.
  • allocate(size: int) -> int allocates size contiguous blocks and returns the starting block index of the allocated region, or -1 if there is not enough contiguous free space.
  • deallocate(startBlock: int, size: int) -> None frees the size contiguous blocks starting at startBlock .
  • freeSpace() -> int returns the total number of free (unallocated) blocks.

Example 1:

Input:
operations = ["DiskSpaceManager", "allocate", "allocate", "freeSpace", "deallocate", "freeSpace", "allocate", "freeSpace"]
arguments  = [[10], [3], [4], [], [0, 3], [], [5], []]

Output:
[null, 0, 3, 3, null, 6, -1, 6]

The disk has 10 blocks. allocate(3) returns 0 and allocate(4) returns 3, leaving blocks 7 to 9 free, so freeSpace() is 3. deallocate(0, 3) frees blocks 0 to 2 and freeSpace() becomes 6. The free regions are now [0, 3) and [7, 10), three blocks each, so allocate(5) returns -1 even though 6 blocks are free in total. Free space stays 6.

Example 2:

Input:
operations = ["DiskSpaceManager", "freeSpace", "allocate", "allocate", "deallocate", "allocate", "freeSpace"]
arguments  = [[5], [], [2], [2], [0, 2], [3], []]

Output:
[null, 5, 0, 2, null, -1, 3]

The disk has 5 blocks, all free. The two allocate(2) calls return 0 and 2. After deallocate(0, 2) the free regions are [0, 2) and [4, 5), so allocate(3) returns -1. Free space is 3.

Example 3:

Input:
operations = ["DiskSpaceManager", "allocate", "allocate", "allocate", "deallocate", "deallocate", "allocate", "freeSpace"]
arguments  = [[8], [2], [3], [2], [0, 2], [2, 3], [4], []]

Output:
[null, 0, 2, 5, null, null, 0, 2]

The three allocations return 0, 2 and 5, leaving only block 7 free. deallocate(0, 2) frees [0, 2). deallocate(2, 3) frees [2, 5), which touches [0, 2), so the two merge into [0, 5). That merged region is what lets allocate(4) return 0. Blocks 4 and 7 remain free, so freeSpace() is 2.

Constraints & Assumptions

  • 1 <= totalBlocks <= 10^5
  • 1 <= size <= totalBlocks for allocate calls
  • 0 <= startBlock < totalBlocks and startBlock + size <= totalBlocks for deallocate calls
  • At most 10^4 operations in total
  • Every block in a deallocate range is currently allocated.

Clarifying Questions to Ask Guidance

  • Does a deallocate call always match one earlier allocation exactly, or can it free part of an allocation or span two adjacent ones?
  • What should happen on a double free or on a range that is already partly free: ignore it, report an error, or is it guaranteed not to happen?
  • How often is freeSpace called compared with the other operations, and should it run in constant time?
  • Is first fit fixed, or should the design make it easy to switch to another placement policy later?

What a Strong Answer Covers Guidance

  • A representation of the free space that supports a first-fit search, and the order it is kept in.
  • Splitting a free region on allocation, including the case where the request uses the whole region.
  • Merging a freed range with free neighbors on either side so that later large requests can fit.
  • How freeSpace is answered and the time and space cost of each operation.
  • What happens on invalid input such as a double free, or a clearly stated precondition.

Follow-up Questions Guidance

  • With many small free regions, first fit becomes a linear scan. How would you make allocate logarithmic in the number of blocks?
  • How would you add best-fit allocation, and how would you index the free regions to support it?
  • How would you detect and reject a double free or a deallocate range that overlaps free space?
  • What changes if callers free by starting block only, without passing the size?