KatInterview

Design a Fixed-Heap Memory Manager

Data Structure DesignModel answer
MediumModel answerData Structure Designmemory-managementdata-structurescomplexity-analysis

Last updated

Build malloc and free over a fixed byte array, comparing allocation strategies and analyzing fragmentation trade-offs.

You are building a memory manager that operates over a single contiguous buffer of fixed size. The manager exposes two operations:

  • allocate(bytes) — returns a pointer to a region of at least bytes usable space, or null if no suitable region exists
  • deallocate(ptr) — marks a previously allocated region as available for reuse

The manager receives its memory up front and cannot request additional memory later. All bookkeeping must happen within the provided buffer.

Your task is to design and implement this manager, starting with a correct baseline and progressing to optimized variants. For each variant, analyze time and space complexity and discuss the trade-offs.

Requirements

  • The buffer is a fixed-size contiguous region initialized once
  • Returned pointers must be aligned to 8-byte boundaries
  • Assume single-threaded execution for the core design
  • You may assume deallocate receives valid pointers from prior allocate calls, but discuss how you would detect misuse

Questions to Consider

Before designing, clarify:

  • What is the typical allocation size distribution?
  • What matters more: allocation speed or memory utilization?
  • How should edge cases like zero-byte requests be handled?
  • Should the design prevent or detect double-free errors?

Part 1: Linear-Scan Allocator

Implement allocate and deallocate using a linear scan: walk through memory until you find the first available region large enough. When a region is larger than needed, divide it so the unused portion remains available.

Describe your block metadata layout, explain how alignment is enforced, and state the time complexity of each operation.

Part 2: Adjacent-Block Merging

Extend deallocate to merge newly-freed regions with adjacent free neighbors. Explain the difference between external and internal fragmentation, and describe how merging addresses one but not the other.

Part 3: Smallest-Fit Strategy

Modify the allocator to choose the smallest free region that satisfies each request, rather than the first one encountered. Select a data structure that makes this efficient.

Explain why a standard min-heap is insufficient for this query, and justify your chosen structure. Describe how block splitting and merging interact with this structure.

Part 4: Strategy Comparison

Compare the linear-scan and smallest-fit approaches across:

  • Allocation and deallocation cost
  • Metadata overhead
  • Long-term fragmentation behavior

Identify workloads where each strategy is preferable.

Hints

Hint 1

Consider what metadata each block needs to store and where that metadata lives relative to the returned pointer

Hint 2

Merging adjacent blocks requires walking backward and forward from the freed block — think about what information enables constant-time neighbor lookups

Hint 3

A query for 'smallest block ≥ X bytes' is a range query, not a global minimum — choose a structure that supports efficient lower-bound search