KatInterview

Semester Planner Plus Burst-Limited Request Filter

Trees & GraphsAuto-graded · 26 test cases
HardAuto-gradedTrees & Graphstopological-sortgraphcycle-detection

Last updated

Combine topological ordering to find the minimum number of semesters (or detect a cycle) with a deque-based simulation of a two-rule request throttle that reports dropped timestamps.

Implement one function that answers two independent questions and returns the results together in a dictionary.

Part 1: Semester planning

There are n courses numbered 0 through n - 1. Each entry [a, b] in prerequisites means course b must be finished in a semester strictly before the semester in which course a is taken.

  • Every course takes exactly one semester.
  • You may take any number of courses in one semester, provided all of their prerequisites were completed in earlier semesters.

Determine:

  • whether all courses can be completed at all (this fails if the prerequisites contain a cycle, including a course that requires itself), and
  • the smallest number of semesters needed to complete every course. If completing all courses is impossible, this value is -1.

Part 2: Throttled request log

You are given requests, a non-decreasing list of arrival times in whole seconds. The index of an entry is the request's ID, and the value is its arrival time. Several requests may share the same time.

A throttle examines requests one at a time in index order. A request is dropped if accepting it would break either rule below. Otherwise it is accepted.

  1. At most 3 accepted requests may have the same arrival time t.
  2. For a request at time t, at most 20 accepted requests may have arrival times in the inclusive range [t - 9, t], counting the request itself.

Only accepted requests count toward either rule. A dropped request has no effect on later decisions.

Collect the arrival times of all dropped requests, in the order they appear in requests.

Return value

Return a dictionary with exactly these keys:

  • "can_finish": True if all courses can be completed, otherwise False.
  • "min_semesters": the minimum number of semesters, or -1 if can_finish is False.
  • "dropped_requests": the list of arrival times of dropped requests, in input order.

Constraints

  • 1 <= n <= 10^5
  • 0 <= len(prerequisites) <= 2 * 10^5
  • Each prerequisite pair has two course indices in [0, n - 1]. Duplicate pairs and self-pairs may appear.
  • 1 <= len(requests) <= 10^5
  • 0 <= requests[i] <= 10^9
  • requests is sorted in non-decreasing order.

Examples

Example 1

Input: plan_semesters_and_filter_requests(2, [[1, 0]], [1, 1, 1, 1, 2])

Output: {'can_finish': True, 'min_semesters': 2, 'dropped_requests': [1]}

Example 2

Input: plan_semesters_and_filter_requests(4, [[1, 0], [2, 1], [3, 2]], [0, 0, 0, 0, 1, 2, 3])

Output: {'can_finish': True, 'min_semesters': 4, 'dropped_requests': [0]}

Example 3

Input: plan_semesters_and_filter_requests(3, [[0, 1], [1, 2], [2, 0]], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

Output: {'can_finish': False, 'min_semesters': -1, 'dropped_requests': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}

Hints

Hint 1

Treat each pair [a, b] as a directed edge b -> a. Courses with no unmet prerequisites can be taken right away; repeatedly remove them and update their dependents (Kahn's algorithm).

Hint 2

While removing courses, track for each course the earliest semester it can be taken: one more than the largest semester among its prerequisites. If fewer than n courses are ever removed, a cycle exists.

Hint 3

For the throttle, keep a deque of accepted timestamps and a per-second count of accepted requests. Before each decision, pop timestamps older than t - 9 from the front, then check the two limits (3 at this second, 20 in the window).