Skip to main content
medium

Design Memory Allocator

Also known as: Design Memory Allocator (Custom)

Design a memory allocator - like how an OS manages RAM! Memory is an array of n units. 0 means free, non-zero means allocated. allocate(size, m_id): Find first contiguous free block, mark with m_id query(): Return total allocated units Example: allocate(3, 1) → 0, query() → 3

This one rewards writing clear, step-by-step logic you can trust. Interviewers love it because it reveals your step-by-step correctness and your edge-case discipline. The classic mistake is a tiny ordering detail; write it like you're debugging a state machine.

DesignSimulation

Examples

You write

Allocator: allocate(size: int, m_id: int), query()

  1. Example 1

    Input
    Allocate 3 units in size 10
    returns
    3
  2. Example 2

    Input
    Allocate twice, check total
    returns
    5

The pattern behind it

Design

I reach for it when

  • The statement describes a class with methods, not one function.
  • Each method needs to be fast, and the structure decides how fast.

Say this before you type. Say each method, the cost you want from it, and the structure that earns that cost.

All 27 Design problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Sequential Allocationmedium
2Part 2
3Part 3

Each locked part opens in the editor once the part before it passes.