Best-Fit Device Memory Pool With Free Block Coalescing

Last updated: November 29, 2026

Quick Overview

Implement a sub-allocator over one large device buffer with malloc(nbytes) returning an aligned offset using best fit, and free(offset) returning the block and merging it with adjacent free blocks.

NVIDIA
Coding & Algorithms
Software Engineer
NVIDIA
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Implement a sub-allocator over one large device buffer with malloc(nbytes) returning an aligned offset using best fit, and free(offset) returning the block and merging it with adjacent free blocks.

A design-a-data-structure question for NVIDIA teams behind the CUDA stream-ordered allocator, RAPIDS memory manager and framework caching allocators, where calling cudaMalloc per tensor is far too slow. The interviewer checks the choice of indexes (by size for best fit, by address for coalescing), correct splitting and merging, and fragmentation trade-offs.

What the Interviewer Expects
  • Keeps free blocks indexed both by (size, offset) for best fit and by offset for neighbor lookup
  • Rounds requests up to the alignment and splits oversized blocks
  • Coalesces with both left and right neighbors on free
  • Keeps the two indexes consistent on every insert and remove
  • Discusses fragmentation, best fit vs first fit, and thread safety
Key Topics to Cover
design a data structure
sorted containers
best fit allocation
coalescing
fragmentation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you make this safe for many host threads, and what about stream ordering on the device?
  • When is a size-class (slab) allocator better than best fit?
  • How would you detect double frees and leaks in a debug build?
  • How would you grow the pool when it is exhausted without moving live allocations?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

MemoryPool(capacity, alignment=256). malloc(n) returns an offset into the buffer or None if no free block fits. Sizes round up to the alignment so every offset stays aligned. free(off) release...

High-Level Design

Two views of the free list. by_size is sorted (size, offset) pairs; a binary search for (need, -1) finds the best fit. free_offsets plus a free_size dict index by address, so on free we can ...


Submit Your Answer
Markdown supported

Related Questions