Airbnb questions

Working Hours Register

AirbnbOnline assessmentMedium

You are building a register of employee working hours as the class WorkingHoursRegister.

Part 1: Check-ins and worked time

Employees check in and out of work. Implement:

  • add_employee(employee_id, position, rate) adds an employee with a unique id, a position and a rate, and returns null.
  • enter(employee_id, time) records that the employee checks in at time and returns null.
  • exit(employee_id, time) records that the employee checks out at time, ending the session that began at their last check-in, and returns null. The session lasts time minus the check-in time.
  • get_worked_time(employee_id) returns the employee's worked time: the total length of their finished sessions. A session that has not been ended yet counts for nothing.

Times are whole numbers in one unit.

  1. Example 1
    init
    []
    operations
    [["add_employee",["e1","dev",10]],["enter",["e1",3]],["get_worked_time",["e1"]],["exit",["e1",8]],["get_worked_time",["e1"]],["add_employee",["e2","qa",7]],["get_worked_time",["e2"]],["enter",["e2",0]],["exit",["e2",0]],["enter",["e2",10]],["exit",["e2",14]],["enter",["e2",20]],["exit",["e2",21]],["get_worked_time",["e2"]],["get_worked_time",["e1"]]]
    Output
    [null,null,0,null,5,null,0,null,null,null,null,null,null,5,5]

    Why: e1 checks in at 3, and while the session is open the worked time is 0. After checking out at 8 it is 5. e2 has three finished sessions of lengths 0, 4 and 1, so 5; before the first check-in it was 0.

Constraints

  • 0 <= time <= 10^9; every employee is added before any other call names them, and ids are added only once
  • For each employee, calls alternate enter and exit, starting with enter, and a call's time is at least the time of that employee's previous call. Times of different employees are not related
  • 1 <= len(employee_id) <= 32, and an id uses lowercase letters and digits
  • 1 <= len(position) <= 32, and a position uses lowercase letters and underscores
  • 0 <= rate <= 10^4
  • At most 2000 calls in total
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Working Hours Register: Airbnb interview question | LoopCoach