Skip to main content
All Questions

Implement a host tracker: allocate(hostType) reserves and returns the hostname with the smallest free number for that type, from 1; deallocate(hostname) frees it for reuse.

Unlock detailed company stats for this questionUpgrade
Hostnames are a host type made of letters followed by a number, such as cache1 or cache2. Implement a tracker with two operations. allocate(hostType) reserves the free hostname of that type with the smallest number, counting from 1, and returns the full hostname. deallocate(hostname) releases that hostname so that it can be given out again; a deallocate for a hostname that is not currently allocated is a valid call and must be handled (it leaves that name free). The operations arrive as a list of strings, 'A <hostType>' for allocate and 'D <hostname>' for deallocate, and the output is the list of hostnames returned by the allocate calls, in order. There are at most 1,000 operations, and host numbers in deallocate requests do not exceed 999. Example: A cache, A cache, A queue, D cache1, A cache, A cache gives cache1, cache2, queue1, cache1, cache3.

Community answers

No answers contributed by the community yet.

Related courses

Course

Behavioral Interviews: A Practical Guide for Engineers

Get offers at the level you deserve. Prep for senior+ behavioral interviews at FAANG and Tier-1 companies. Made with staff and principal engineers from Meta, Netflix, Google, and more. Avoid downleveling, elevate past projects, and stop sounding like a robot.

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.