Design Phone Directory

MediumDesignheap

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A phone system owns the numbers 0 through maxNumbers - 1, all initially unassigned. Implement PhoneDirectory: - get() assigns and returns the **smallest** unassigned number, or -1 if every number is taken. - check(number) returns true if number is currently unassigned, false otherwise. - release(number) makes number unassigned again. Releasing a number that is already unassigned does nothing. Because get always hands out the smallest free number, the sequence of results is fully determined.

Examples

Input: ["PhoneDirectory","get","get","check","get","check","release","check","get"] [[3],[],[],[2],[],[2],[2],[2],[]]
Output: [null,0,1,true,2,false,null,true,2]
The first three gets hand out 0, 1, 2. Number 2 is free before the third get and taken after it. Releasing 2 frees it, so the last get returns 2 again.
Input: ["PhoneDirectory","get","get","release","release","get","check"] [[1],[],[],[0],[0],[],[0]]
Output: [null,0,-1,null,null,0,false]
Only number 0 exists. The second get fails with -1. Releasing 0 twice is harmless, and the next get reassigns it.

Constraints

  • 1 <= maxNumbers <= 10^4
  • 0 <= number < maxNumbers
  • At most 2 * 10^4 calls in total

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More heap problems