Allocate the Smallest Free Worker Id With Releases

Last updated: November 29, 2026

Quick Overview

Implement an allocator that hands out integer worker ids from 0 to max_ids - 1, always returning the smallest id not currently in use, supports releasing ids, and returns -1 when all ids are taken.

OpenAI
Coding & Algorithms
Software Engineer
OpenAI
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Implement an allocator that hands out integer worker ids from 0 to max_ids - 1, always returning the smallest id not currently in use, supports releasing ids, and returns -1 when all ids are taken.

Inference and training fleets at OpenAI assign compact ranks or slot ids to workers, and reusing the lowest free slot keeps tables dense and logs readable. This warm-up checks whether you pick a heap plus a high-water mark rather than scanning, and whether you guard against double release.

What the Interviewer Expects
  • Avoid an O(n) scan per allocation
  • Use a high-water mark for never-issued ids plus a min-heap for released ones
  • Explain why released ids are always smaller than the high-water mark
  • Reject releasing an id that is not allocated
  • State per-operation complexity
Key Topics to Cover
heap
id allocation
hash set
amortized analysis
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Support allocating a specific id on request: how does the lazy heap need to change?
  • Allocate a contiguous block of k ids: which data structure fits?
  • Make it safe across threads, then across processes.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • allocate(): smallest free id in [0, max_ids) or -1 when exhausted.
  • release(id): returns the id to the pool; releasing a free id is an error.
  • Operations should be logarithmic or better, n...
High-Level Design

Two sources of free ids exist: ids never handed out (all at or above next_fresh) and ids handed back (a min-heap). Every released id is below next_fresh, so if the heap is non-empty, its minimum i...


Submit Your Answer
Markdown supported