call(back)
Algorithms & Data Structuresmedium

Design a Smallest-Free ID Allocator

Design an ID allocator over the space [0, capacity):

a = new IDAllocator(1000)
a.allocate()        // -> smallest available ID, or -1 if none
a.release(id)       // -> true if id was allocated; false otherwise
                    //    (double-release, never-allocated)
a.setCapacity(c)    // capacity is ADJUSTABLE at runtime, up or down

Shrinking must not invalidate IDs already handed out — an ID beyond the new capacity stays valid until released; it just cannot be re-allocated while capacity is below it. If capacity grows back, previously released high IDs become allocatable again.

Asked at