Problem · Hash Table

Calendar Event System with Pagination and Intersection

Learn this problem
MediumLead BankONSITE INTERVIEW

Problem statement

Implement a calendar that stores active named events. Process each row in operations and append one string array to the result:

  • ["INSERT", name, start, end]: Insert the half-open event interval [start, end). Active event names are unique. Return ["true"] on success or ["false"] if that name already exists.
  • ["DELETE", name]: Delete the active event with that name. Return ["true"] on success or ["false"] if it does not exist. A deleted name may be inserted again later.
  • ["LIST", pageSize, pageNum]: Sort all active events by start time, then end time, then name, all ascending. Return the names on the one-based page. If that page starts beyond the list, return an empty array.
  • ["INTERSECT", queryStart, queryEnd]: Return the names of all active events whose half-open intervals intersect [queryStart, queryEnd), in the same sorted order. Two intervals intersect exactly when eventStart < queryEnd and queryStart < eventEnd.

Different events may overlap. Return one row per operation in input order.

Function

processCalendarOperations(operations: String[][]) → String[][]

Examples

Example 1

operations = [["INSERT","standup","10","11"],["INSERT","focus","9","12"],["INSERT","lunch","12","13"],["LIST","2","1"],["INTERSECT","10","12"],["DELETE","standup"],["LIST","5","1"]]return = [["true"],["true"],["true"],["focus","standup"],["focus","standup"],["true"],["focus","lunch"]]

Sorted order is focus, standup, lunch. The interval [10, 12) intersects the first two events but not lunch, which starts exactly at the query's excluded endpoint. After deleting standup, two events remain.

Example 2

operations = [["INSERT","a","1","3"],["INSERT","b","3","5"],["INSERT","c","2","4"],["LIST","1","2"],["INTERSECT","3","4"],["DELETE","missing"],["INSERT","a","6","7"]]return = [["true"],["true"],["true"],["c"],["c","b"],["false"],["false"]]

The second one-item page contains c. Event a ends at 3 and therefore does not intersect [3, 4); c and b do. Deleting an absent name and reinserting an active name both fail without changing state.

Constraints

  • 1 <= operations.length <= 10^4
  • Event names are non-empty and contain only ASCII letters, digits, hyphens, or underscores.
  • Every time is an integer in [0, 10^9].
  • Every inserted event and intersection query satisfies start < end.
  • Every pageSize and pageNum is a positive integer.
  • Every operation has one of the documented shapes.

More Lead Bank problems

drafts saved locally
public String[][] processCalendarOperations(String[][] operations) {
    // write your code here
}
operations[["INSERT","standup","10","11"],["INSERT","focus","9","12"],["INSERT","lunch","12","13"],["LIST","2","1"],["INTERSECT","10","12"],["DELETE","standup"],["LIST","5","1"]]
expected[["true", "true", "true", "focus", "standup", "focus", "standup", "true", "focus", "lunch"]]
checking account