Server Numbers
Start Timer
0:00:00
Let’s say we’re managing a fleet of servers of different types, like web, db, and cache. Each type numbers its own servers starting at 1.
Write a class called ServerAllocator with an allocate(server_type) method that returns the lowest number not currently in use for that type, and a deallocate(server_type, number) method that frees a number so it can be handed out again.
Note: Both methods should be faster than scanning every number in use. Raise a ValueError if the number being deallocated is not in use.
Example:
a = ServerAllocator()
a.allocate("web") # 1
a.allocate("web") # 2
a.allocate("db") # 1
a.deallocate("web", 1)
a.allocate("web") # 1
a.allocate("web") # 3
.
.
.
.
.
.
.
.
.
Comments