Skip to content

Instantly share code, notes, and snippets.

@TheSithPadawan
Created April 10, 2022 03:56
Show Gist options
  • Save TheSithPadawan/0c5fc9242f3978679e0dfb3bea658a78 to your computer and use it in GitHub Desktop.
Save TheSithPadawan/0c5fc9242f3978679e0dfb3bea658a78 to your computer and use it in GitHub Desktop.
Amazon Locker OO Design
from enum import ENUM
class LockerSize(ENUM):
# for example.. not necessarily a cube
SMALL = (1, 1, 1)
MEDIUM = (2, 2, 2)
LARGE = (3, 3, 3)
class Locker:
def __init__(self, id, size, **kwargs):
self.id = id
self.size = size
self.dimensions = sorted(self.size.value)
self.occupied = False
class Package:
def __init__(self, x, y, z, **kwargs):
self.length = x
self.width = y
self.height = z
self.package_id = kwargs.get('id', None)
self.dimensions = sorted([x, y, z])
class LockerFinder:
def __init__(self):
self.lockers = []
def register_locker(self, locker):
self.lockers.append(locker)
def find_locker_for_package(self, package):
"""
simple algo to find locker. clarify with interviewer if he wants you to implement something fancy.
E.g. binary search over largest dimensions, then check the remaining two dimensions, etc
iterate over all lockers and find the first that is available and fit
returns -1 if not found
"""
for locker in self.lockers:
if locker.occupied:
continue
if locker.dimensions[0] >= package.dimensions[0] and locker.dimensions[1] >= package.dimensions[1] and locker.dimensions[2] >= package.dimensions[2]
locker.occupied = True
return locker.id
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment