PracHub
QuestionsCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/OpenAI

Implement A Contiguous Memory Allocator With Free-Block Coalescing

Last updated: Jul 8, 2026

Quick Overview

Review a contiguous memory allocator coding prompt with malloc, free, first-fit allocation, block coalescing, and a best-fit follow-up. The interview topic emphasizes interval management, allocated-block maps, fragmentation, secondary indexes, and complexity trade-offs.

  • medium
  • OpenAI
  • Coding & Algorithms
  • Software Engineer

Implement A Contiguous Memory Allocator With Free-Block Coalescing

Company: OpenAI

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Implement a memory manager for a contiguous block of `N` bytes. The memory manager starts with all bytes free and must support: ```python malloc(k: int) -> int free(ptr: int) -> None ``` `malloc(k)` allocates one physically contiguous block of `k` bytes and returns the starting offset of the allocated block. If no contiguous free block can fit `k` bytes, return `-1`. `free(ptr)` frees the block that starts at `ptr`. The caller only provides the starting offset, so your allocator must remember the size of each allocated block. When adjacent free blocks become neighbors after a free, merge them into one larger free block. Start with a first-fit allocator. Then describe how you would change the design to support best-fit allocation efficiently. Function signature: ```python class MemoryAllocator: def __init__(self, n: int): pass def malloc(self, k: int) -> int: pass def free(self, ptr: int) -> None: pass ``` Constraints: - Allocations must be contiguous. - Allocated blocks cannot be moved after `malloc` returns. - `free(ptr)` is called only for currently allocated block starts unless the interviewer asks for defensive handling. - The first-fit version may keep free blocks ordered by start offset. - The best-fit follow-up should explain a secondary index ordered by block size. Examples: ```text allocator = MemoryAllocator(10) allocator.malloc(3) -> 0 allocator.malloc(4) -> 3 allocator.free(0) allocator.malloc(2) -> 0 allocator.free(3) At this point the free ranges should be coalesced where adjacent. ```

Quick Answer: Review a contiguous memory allocator coding prompt with malloc, free, first-fit allocation, block coalescing, and a best-fit follow-up. The interview topic emphasizes interval management, allocated-block maps, fragmentation, secondary indexes, and complexity trade-offs.

Related Interview Questions

  • Consistent Hashing Ring with Virtual Nodes for Shard Rebalancing - OpenAI (hard)
  • Infection Spread Simulation with Death Threshold - OpenAI (medium)
  • Spreading Contagion on a Grid - OpenAI (medium)
  • Aggregate Recent Message Events And Active Chats - OpenAI (hard)
  • Streaming Entropy with Numerical Stability - OpenAI (hard)
|Home/Coding & Algorithms/OpenAI

Implement A Contiguous Memory Allocator With Free-Block Coalescing

OpenAI logo
OpenAI
Jun 5, 2026, 12:00 AM
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
3
0

Implement a memory manager for a contiguous block of N bytes.

The memory manager starts with all bytes free and must support:

malloc(k: int) -> int
free(ptr: int) -> None

malloc(k) allocates one physically contiguous block of k bytes and returns the starting offset of the allocated block. If no contiguous free block can fit k bytes, return -1.

free(ptr) frees the block that starts at ptr. The caller only provides the starting offset, so your allocator must remember the size of each allocated block. When adjacent free blocks become neighbors after a free, merge them into one larger free block.

Start with a first-fit allocator. Then describe how you would change the design to support best-fit allocation efficiently.

Function signature:

class MemoryAllocator:
    def __init__(self, n: int):
        pass

    def malloc(self, k: int) -> int:
        pass

    def free(self, ptr: int) -> None:
        pass

Constraints:

  • Allocations must be contiguous.
  • Allocated blocks cannot be moved after malloc returns.
  • free(ptr) is called only for currently allocated block starts unless the interviewer asks for defensive handling.
  • The first-fit version may keep free blocks ordered by start offset.
  • The best-fit follow-up should explain a secondary index ordered by block size.

Examples:

allocator = MemoryAllocator(10)
allocator.malloc(3) -> 0
allocator.malloc(4) -> 3
allocator.free(0)
allocator.malloc(2) -> 0
allocator.free(3)

At this point the free ranges should be coalesced where adjacent.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More OpenAI•More Software Engineer•OpenAI Software Engineer•OpenAI Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.