PracHub
QuestionsLearningGuidesInterview Prep
|Home/Software Engineering Fundamentals/OpenAI

Design First-Fit and Best-Fit Memory Allocation

Last updated: Jul 28, 2026

Quick Overview

Design a contiguous-region heap allocator that supports two placement policies, block splitting, freeing, and adjacent-block coalescing. Explain metadata consistency, exact-fit and exhaustion cases, duplicate or invalid frees, fragmentation, alignment, concurrency, and tests that expose corruption.

  • medium
  • OpenAI
  • Software Engineering Fundamentals
  • Software Engineer

Design First-Fit and Best-Fit Memory Allocation

Company: OpenAI

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Technical Screen

Design the core of a heap allocator over one contiguous managed region. Blocks have an address, size, and free/allocated state. Allocation requests specify a positive byte count; free requests identify a previously allocated block. Adjacent free blocks must be coalesced. ### Part 1: First-Fit Allocation Implement or describe first-fit allocation. Explain the data structures used to find a block, split excess space, insert a newly freed block by address, and merge with both neighbors. #### What This Part Should Cover - Address-ordered block metadata - Split and exact-fit cases - Left and right coalescing - Invalid and duplicate free detection - Time and space complexity ### Part 2: Best-Fit Optimization Add best-fit allocation using an extra index ordered by free-block size. Explain how every split, allocation, free, and merge updates both the address index and size index atomically. #### What This Part Should Cover - Lower-bound search by requested size - Stable handling of equal-size blocks - Cross-links or IDs shared by both indexes - Complexity and fragmentation trade-offs ### Part 3: Testing Design tests that would expose metadata corruption and subtle coalescing bugs, not only successful allocations. #### What This Part Should Cover - Conservation of total managed bytes - Nonoverlap and address ordering - Double-free, unknown pointer, and exhaustion cases - Sequences that merge left, right, and both sides ### What a Strong Answer Covers A strong answer makes the invariants of both indexes explicit and recognizes that best-fit changes search cost but does not guarantee low fragmentation. ### Follow-up Questions - Add alignment requirements. - Make allocation thread-safe. - Compare boundary tags with an external metadata tree.

Quick Answer: Design a contiguous-region heap allocator that supports two placement policies, block splitting, freeing, and adjacent-block coalescing. Explain metadata consistency, exact-fit and exhaustion cases, duplicate or invalid frees, fragmentation, alignment, concurrency, and tests that expose corruption.

Related Interview Questions

  • Clarify and Design Social-Graph Milestones - OpenAI (medium)
  • Implement A Mobile Chat Interface In An Existing Codebase - OpenAI (medium)
  • Count Machines and Recover a Distributed Tree Topology - OpenAI (medium)
  • Implement a Recoverable In-Memory Key-Value Store - OpenAI (medium)
|Home/Software Engineering Fundamentals/OpenAI

Design First-Fit and Best-Fit Memory Allocation

OpenAI logo
OpenAI
Jul 27, 2026, 12:00 AM
mediumSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
1
0

Design the core of a heap allocator over one contiguous managed region. Blocks have an address, size, and free/allocated state. Allocation requests specify a positive byte count; free requests identify a previously allocated block. Adjacent free blocks must be coalesced.

Part 1: First-Fit Allocation

Implement or describe first-fit allocation. Explain the data structures used to find a block, split excess space, insert a newly freed block by address, and merge with both neighbors.

What This Part Should Cover Guidance

  • Address-ordered block metadata
  • Split and exact-fit cases
  • Left and right coalescing
  • Invalid and duplicate free detection
  • Time and space complexity

Part 2: Best-Fit Optimization

Add best-fit allocation using an extra index ordered by free-block size. Explain how every split, allocation, free, and merge updates both the address index and size index atomically.

What This Part Should Cover Guidance

  • Lower-bound search by requested size
  • Stable handling of equal-size blocks
  • Cross-links or IDs shared by both indexes
  • Complexity and fragmentation trade-offs

Part 3: Testing

Design tests that would expose metadata corruption and subtle coalescing bugs, not only successful allocations.

What This Part Should Cover Guidance

  • Conservation of total managed bytes
  • Nonoverlap and address ordering
  • Double-free, unknown pointer, and exhaustion cases
  • Sequences that merge left, right, and both sides

What a Strong Answer Covers Guidance

A strong answer makes the invariants of both indexes explicit and recognizes that best-fit changes search cost but does not guarantee low fragmentation.

Follow-up Questions Guidance

  • Add alignment requirements.
  • Make allocation thread-safe.
  • Compare boundary tags with an external metadata tree.
Loading comments...

Browse More Questions

More Software Engineering Fundamentals•More OpenAI•More Software Engineer•OpenAI Software Engineer•OpenAI Software Engineering Fundamentals•Software Engineer Software Engineering Fundamentals

Write your answer

Your first approved answer each day earns 20 XP.

Sign in to write your answer.
PracHub

Master your tech interviews with 9,000+ real questions from top companies.

Product

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

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.