Implement course scheduling and rate limiter analysis
Problem 1: Course Scheduling
You are planning to take a set of courses and are given prerequisite relationships between them.
- There are
ncourses labeled from0ton - 1. - You are given a list of prerequisite pairs
prerequisites, where each pair[a, b]means you must complete coursebbefore you can take coursea.
Task A
Determine whether it is possible to finish all n courses.
- Return
trueif you can complete all courses (i.e., the prerequisite graph has no cycle); otherwise returnfalse.
You should specify the time and space complexity of your algorithm.
Task B (Follow-up)
Assume:
- Each course takes exactly one semester to complete.
- In any given semester, you may take any number of courses, as long as you have completed all of their prerequisites in earlier semesters.
If it is possible to finish all courses, compute the minimum number of semesters required to complete all n courses. If it is impossible (due to cycles), indicate that it cannot be done.
Describe an efficient algorithm for this and its time and space complexity.
You may assume:
1 <= n <= 10^5.0 <= len(prerequisites) <= 2 * 10^5.
Problem 2: Analyze Dropped Requests Under a Rate Limiter
You are running a social media website that suddenly went viral. To protect your backend, you added a rate limiter. Now you want to analyze logs to determine which requests would be dropped by this limiter.
You are given:
- An array
requests, where the index is the request ID (0-based), and the valuerequests[i]is the time in seconds when thei-th request arrived. - The array
requestsis non-decreasing (requests are logged in arrival order; multiple requests can have the same timestamp if they arrive in the same second).
Your rate limiter enforces the following rules:
- At most 3 requests per second can be processed.
- At most 20 requests per any sliding 10-second window can be processed.
Formally:
- For rule (1): For any timestamp
t, among all requests with arrival time exactlyt, at most 3 can be processed; any additional requests arriving at timetmust be dropped. - For rule (2): For any request arriving at time
t, consider all requests with arrival times in the interval[t - 9, t](inclusive). If processing this request would cause the total number of processed requests in that interval to exceed 20, then this request must be dropped.
Rules are applied in arrival order:
- For each request in index order, first consider whether it is dropped due to rule (1) or rule (2), taking into account only previously processed requests (not the dropped ones).
- If a request is dropped by either rule, it is not counted as processed for any future checks.
Task
Given the array requests, return a list of the times of all dropped requests, in the order they appear in the input.
You should:
- Clearly define the algorithm you use to determine dropped requests.
- Aim for an efficient solution that can handle up to
10^5requests. - State the time and space complexity of your approach.
You may assume:
1 <= len(requests) <= 10^5.0 <= requests[i] <= 10^9.requestsis non-decreasing.
AI interview prep
Core dilemma
Demonstrating mastery of fundamental graph algorithms (cycle detection, topological sort, level calculation) and efficient sliding window techniques, while meticulously managing state and adhering to complex, interdependent rules under strict performance constraints.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement course scheduling and rate limiter analysis
Problem 1: Course Scheduling
You are planning to take a set of courses and are given prerequisite relationships between them.
- There are
ncourses labeled from0ton - 1. - You are given a list of prerequisite pairs
prerequisites, where each pair[a, b]means you must complete coursebbefore you can take coursea.
Task A
Determine whether it is possible to finish all n courses.
- Return
trueif you can complete all courses (i.e., the prerequisite graph has no cycle); otherwise returnfalse.
You should specify the time and space complexity of your algorithm.
Task B (Follow-up)
Assume:
- Each course takes exactly one semester to complete.
- In any given semester, you may take any number of courses, as long as you have completed all of their prerequisites in earlier semesters.
If it is possible to finish all courses, compute the minimum number of semesters required to complete all n courses. If it is impossible (due to cycles), indicate that it cannot be done.
Describe an efficient algorithm for this and its time and space complexity.
You may assume:
1 <= n <= 10^5.0 <= len(prerequisites) <= 2 * 10^5.
Problem 2: Analyze Dropped Requests Under a Rate Limiter
You are running a social media website that suddenly went viral. To protect your backend, you added a rate limiter. Now you want to analyze logs to determine which requests would be dropped by this limiter.
You are given:
- An array
requests, where the index is the request ID (0-based), and the valuerequests[i]is the time in seconds when thei-th request arrived. - The array
requestsis non-decreasing (requests are logged in arrival order; multiple requests can have the same timestamp if they arrive in the same second).
Your rate limiter enforces the following rules:
- At most 3 requests per second can be processed.
- At most 20 requests per any sliding 10-second window can be processed.
Formally:
- For rule (1): For any timestamp
t, among all requests with arrival time exactlyt, at most 3 can be processed; any additional requests arriving at timetmust be dropped. - For rule (2): For any request arriving at time
t, consider all requests with arrival times in the interval[t - 9, t](inclusive). If processing this request would cause the total number of processed requests in that interval to exceed 20, then this request must be dropped.
Rules are applied in arrival order:
- For each request in index order, first consider whether it is dropped due to rule (1) or rule (2), taking into account only previously processed requests (not the dropped ones).
- If a request is dropped by either rule, it is not counted as processed for any future checks.
Task
Given the array requests, return a list of the times of all dropped requests, in the order they appear in the input.
You should:
- Clearly define the algorithm you use to determine dropped requests.
- Aim for an efficient solution that can handle up to
10^5requests. - State the time and space complexity of your approach.
You may assume:
1 <= len(requests) <= 10^5.0 <= requests[i] <= 10^9.requestsis non-decreasing.