Design Amazon Locker system
Anonymous User
46747

Given

  • Users should be able to use a code to open a locker and pick up a package
  • Delivery guy should be able to find an "optimal" locker for a package

Then

  • Free coding from scratch in any language

What I did

I used Vec<Locker> to represent the state.

The interviewer asked if I can improve the code.

I told I could probably use a HashMap (Size -> Locker) if there are standard sizes like (small, medium, large instead of (w,h,d))

Got rejected

trait Volumable {
    fn get_volume(&self) -> u32;
}

#[derive(Debug, PartialEq)]
pub struct Package {
    w: u32,
    h: u32,
    d: u32,
}

impl Volumable for Package {
    fn get_volume(&self) -> u32 {
        self.w * self.h * self.d
    }
}

#[derive(Debug, PartialEq)]
pub struct Locker {
    w: u32,
    h: u32,
    d: u32,

    code: u32,
    occupied: bool,
}

impl Volumable for Locker {
    fn get_volume(&self) -> u32 {
        self.w * self.h * self.d
    }
}

pub fn get_optimal_locker<'a>(
    lockers: &'a Vec<Locker>,
    package: &Package,
) -> Option<&'a Locker> {
    lockers
        .iter()
        .filter(|l| !l.occupied)
		
		// simplifying here but I wrote a custom fit function to check (w, h, d)
        .filter(|l| package.get_volume() <= l.get_volume())
		
        .min_by_key(|l| l.get_volume())
}

pub fn get_locker_by_code<'a>(lockers: &'a Vec<Locker>, code: u32) -> Option<&'a Locker> {
    lockers.iter().find(|l| l.code == code)
}
Comments (13)