Skip to content

Instantly share code, notes, and snippets.

@stealthbomber10
Created October 10, 2018 19:01
Show Gist options
  • Save stealthbomber10/d85d44776ad58ba66d84ff76fd5be736 to your computer and use it in GitHub Desktop.
Save stealthbomber10/d85d44776ad58ba66d84ff76fd5be736 to your computer and use it in GitHub Desktop.
stripe interview
from collections import defaultdict
import string
# You're running a pool of servers where the servers are numbered sequentially starting from 1. Over time, any given server might explode, in which case its server number is made available for reuse. When a new server is launched, it should be given the lowest available number.
# Write a function which, given the list of currently allocated server numbers, returns the number of the next server to allocate. In addition, you should demonstrate your approach to testing that your function is correct. You may choose to use an existing testing library for your language if you choose, or you may write your own process if you prefer.
# For example, your function should behave something like the following:
# >> next_server_number([5, 3, 1])
# 2
# >> next_server_number([5, 4, 1, 2])
# 3
# >> next_server_number([3, 2, 1])
# 4
# >> next_server_number([2, 3])
# 1
# >> next_server_number([])
# 1
# >> next_server_number([1,1.5,2,2.5,3,3.5,4,5,5.5])
# 6
# >> next_server_number([2.5])
def next_server_number(current_numbers):
'''function to allocate new server numbers'''
if not current_numbers:
return 1
else:
highest_number = max(current_numbers)
for num in range(1,highest_number):
if num not in current_numbers:
return num
return highest_number + 1
# print(next_server_number([5,3,1]))
# print(next_server_number([5,4,1,2]))
# print(next_server_number([3,2,1]))
# print(next_server_number([2,3]))
# print(next_server_number([]))
# Server names consist of an alphabetic host type (e.g. "apibox") concatenated with the server number, with server numbers allocated as before (so "apibox1", "apibox2", etc. are valid hostnames).
# Write a name tracking class with two operations, allocate(host_type) and deallocate(hostname). The former should reserve and return the next available hostname, while the latter should release that hostname back into the pool.
# For example:
# >> tracker = Tracker()
# >> tracker.allocate("apibox")
# "apibox1"
# >> tracker.allocate("apibox")
# "apibox2"
# >> tracker.deallocate("apibox1")
# nil
# >> tracker.allocate("apibox")
# "apibox1"
# >> tracker.allocate("sitebox")
# "sitebox1
# >> tracker.allocate("#$@%")
class Tracker:
def __init__(self):
self.host_types = defaultdict(list)
def allocate(self, host_type):
'''function to allocate new server numbers'''
available = self.next_server_number(self.host_types[host_type])
# print(self.host_types)
self.host_types[host_type].append(available)
return '{}{}'.format(host_type, available)
def deallocate(self, hostname):
'''function to deallocate the inputted server'''
last_letter = 0
for (i,char) in enumerate(hostname):
if char in string.ascii_letters:
last_letter = i
host_num = hostname[last_letter+1:]
host_type = hostname[:last_letter+1]
# print(host_num)
# print(self.host_types)
self.host_types[host_type].remove(int(host_num))
return None
def next_server_number(self, current_numbers):
'''function to detect lowest available server number'''
if not current_numbers:
return 1
else:
highest_number = max(current_numbers)
for num in range(1,highest_number):
if num not in current_numbers:
return num
return highest_number + 1
tracker = Tracker()
print(tracker.allocate("apibox"))
print(tracker.allocate("apibox"))
print(tracker.deallocate("apibox1"))
print(tracker.allocate("apibox"))
print(tracker.allocate("sitebox"))
@abc5051001
Copy link

Need to put this line in deallocate in the case where the hostname given isn't in self.host_types[host_num]
if int(host_num) not in self.host_types[host_type]:
return None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment