FastPrepCount Active Employee Access Grants

Count Active Employee Access Grants

IBM logoIBM● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Each employee has an access duration. An event (employeeId, time) starts or renews that employee's access through time + duration. Access intervals are half-open: an expiration equal to a timestamp is inactive at that timestamp unless a renewal occurs exactly then.

After an employee first receives access, every later event for that employee must occur no later than the current expiration. If an event arrives after a gap, that employee becomes permanently invalid and later events cannot restore access.

Given the employee definitions and a nondecreasing event stream, return how many employees remain valid and active at the timestamp of the final event.

Function

countActiveAccess(employeeIds: String[], durations: int[], eventEmployeeIds: String[], eventTimes: int[]) → int

Examples

Example 1

employeeIds = ["a","b"]durations = [5,2]eventEmployeeIds = ["a","b","a"]eventTimes = [1,3,5]return = 1

At time 5, employee a is active until 10. Employee b expires exactly at 5 and is inactive.

Example 2

employeeIds = ["x","y"]durations = [3,10]eventEmployeeIds = ["x","y"]eventTimes = [2,4]return = 2

At the final time 4, the grants expire at 5 and 14.

Example 3

employeeIds = ["x"]durations = [4]eventEmployeeIds = ["x","x"]eventTimes = [1,3]return = 1

The second event extends the expiration from 5 to 7.

Example 4

employeeIds = ["x"]durations = [2]eventEmployeeIds = ["x","x","x"]eventTimes = [1,4,5]return = 0

The first grant expires at time 3. The event at time 4 follows a gap, so employee x is permanently invalid and the event at time 5 cannot restore access.

Constraints

  • 1 <= employeeIds.length = durations.length <= 200000.
  • Employee identifiers are unique non-empty strings.
  • 1 <= durations[i] <= 10^9.
  • 1 <= eventEmployeeIds.length = eventTimes.length <= 200000.
  • Every event employee exists in employeeIds, and eventTimes is nondecreasing.
  • A renewal at the exact current expiration is continuous; a later renewal permanently invalidates that employee.
  • Each expiration fits in a signed 64-bit integer.

More IBM problems

See IBM hiring insights
public int countActiveAccess(String[] employeeIds, int[] durations, String[] eventEmployeeIds, int[] eventTimes) {
    // Write your code here.
}
employeeIds["a","b"]
durations[5,2]
eventEmployeeIds["a","b","a"]
eventTimes[1,3,5]
expected1
Checking account…