You are given a 2D 0-indexed array of strings, access_times
, with size n
. For each i
where 0 <= i <= n - 1
, access_times[i][0]
represents the name of an employee, and access_times[i][1]
represents the access time of that employee. All entries in access_times
are within the same day.
The access time is represented as four digits using a 24-hour time format, for example, "0800"
or "2250"
.
An employee is said to be high-access if he has accessed the system three or more times within a one-hour period.
Times with exactly one hour of difference are not considered part of the same one-hour period. For example, "0815"
and "0915"
are not part of the same one-hour period.
Access times at the start and end of the day are not counted within the same one-hour period. For example, "0005"
and "2350"
are not part of the same one-hour period.
Return a list that contains the names of high-access employees with any order you want.
Example 1:
Input: access_times = [["a","0549"],["b","0457"],["a","0532"],["a","0621"],["b","0540"]] Output: ["a"] Explanation: "a" has three access times in the one-hour period of [05:32, 06:31] which are 05:32, 05:49, and 06:21. But "b" does not have more than two access times at all. So the answer is ["a"].
Example 2:
Input: access_times = [["d","0002"],["c","0808"],["c","0829"],["e","0215"],["d","1508"],["d","1444"],["d","1410"],["c","0809"]] Output: ["c","d"] Explanation: "c" has three access times in the one-hour period of [08:08, 09:07] which are 08:08, 08:09, and 08:29. "d" has also three access times in the one-hour period of [14:10, 15:09] which are 14:10, 14:44, and 15:08. However, "e" has just one access time, so it can not be in the answer and the final answer is ["c","d"].
Example 3:
Input: access_times = [["cd","1025"],["ab","1025"],["cd","1046"],["cd","1055"],["ab","1124"],["ab","1120"]] Output: ["ab","cd"] Explanation: "ab" has three access times in the one-hour period of [10:25, 11:24] which are 10:25, 11:20, and 11:24. "cd" has also three access times in the one-hour period of [10:25, 11:24] which are 10:25, 10:46, and 10:55. So the answer is ["ab","cd"].
Constraints:
1 <= access_times.length <= 100
access_times[i].length == 2
1 <= access_times[i][0].length <= 10
access_times[i][0]
consists only of English small letters.access_times[i][1].length == 4
access_times[i][1]
is in 24-hour time format.access_times[i][1]
consists only of '0'
to '9'
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding high-access employees means we'll check every possible combination of employees and times. We'll look at each employee and see if they meet the access criteria by checking all their access times against everyone else's.
Here's how the algorithm would work step-by-step:
def high_access_employees_brute_force(access_times):
high_access_employees = set()
for employee_access_data in access_times:
employee_id = employee_access_data[0]
access_time = int(employee_access_data[1])
access_count = 0
#Check every other employee and their access times
for other_employee_access_data in access_times:
other_employee_id = other_employee_access_data[0]
other_access_time = int(other_employee_access_data[1])
#We need to exclude comparing the same access time to itself.
if employee_id == other_employee_id and access_time == other_access_time:
continue
#Check the access time window to count employees within it
if abs(access_time - other_access_time) < 60:
access_count += 1
if access_count >= 2:
#Mark employee if the access count meets criteria
high_access_employees.add(employee_id)
\
return list(high_access_employees)
The problem requires finding employees who have access to the system at the same time as other employees. We can efficiently solve it by tracking how many employees are actively logged in at any moment and pinpointing times when the count is high. This avoids checking every possible combination of employees.
Here's how the algorithm would work step-by-step:
def find_high_access_employees(access_times, threshold):
timeline_events = []
for employee, start_time, end_time in access_times:
timeline_events.append((start_time, 'start', employee))
timeline_events.append((end_time, 'end', employee))
timeline_events.sort()
active_employees = set()
high_access_employees = set()
employee_count = 0
for time, event_type, employee in timeline_events:
if event_type == 'start':
# Increment employee count when an employee logs in
employee_count += 1
active_employees.add(employee)
if employee_count > threshold:
# If count exceeds threshold, add logged in employees
high_access_employees.update(active_employees)
else:
# Decrement employee count when an employee logs out
employee_count -= 1
active_employees.remove(employee)
return list(high_access_employees)
Case | How to Handle |
---|---|
Empty access_times list | Return an empty list since there are no employees to analyze. |
Access times list with only one employee | Return an empty list, as no pair of employees can be compared. |
Very large access_times list (scalability) | The solution should use efficient data structures (hash maps) and algorithms (sorting) to avoid exceeding time limits. |
All employees have identical access times | The code should correctly identify those employees who have high access given the same times. |
Extreme access times (start or end of the day) | Ensure the comparison logic handles wrap-around cases correctly, accounting for the 24-hour clock. |
Multiple sets of employees exceed the access threshold | The solution should return all employees who meet the high-access criteria, not just the first found set. |
Input access times are not sorted | Sort the access times for each employee to make comparison efficient; if sort is not applied, then the solution would incorrectly report employees. |
Access times are invalid or out of range | Validate the access times to be within reasonable range (00:00 to 23:59) and throw or log an error if invalid. |