Run Tasks with a Concurrency Limit
Updated
Problem statement
A helper like mapLimit runs an async job for each item of a list, but never more than limit jobs at the same time. Here the items come in as a stream: item i arrives at time arrivals[i], and its job takes durations[i] time units.
Jobs start in input order. Job i starts at the earliest time when all of these are true:
- item
ihas arrived, - job
i - 1has started (wheni > 0), and - fewer than
limitjobs are running.
A job that starts at time s runs until s + durations[i]. A job that finishes at time T is no longer running at T, so its slot can start another job at T. When several jobs can start at the same moment, they start in input order.
Return one row [start, finish] for each job, in input order.
Function
scheduleWithLimit(arrivals: int[], durations: int[], limit: int) → int[][]Examples
Example 1
arrivals = [0,0,0,0,0]durations = [4,2,3,1,5]limit = 2return = [[0,4],[0,2],[2,5],[4,5],[5,10]]All five jobs are ready at time 0, and two may run at once. Jobs 0 and 1 start at 0. Job 1 ends at 2, so job 2 starts at 2 and ends at 5. Job 0 ends at 4, so job 3 starts at 4 and ends at 5. Two jobs end at 5, and job 4 starts then and ends at 10. All work is done at time 10.
Example 2
arrivals = [0,1,1,6,6]durations = [3,3,1,2,2]limit = 2return = [[0,3],[1,4],[3,4],[6,8],[6,8]]Job 0 starts at 0 and job 1 starts when it arrives at 1. Job 2 also arrives at 1, but both slots are busy until job 0 ends at 3, so it runs from 3 to 4. Nothing runs from 4 to 6. Jobs 3 and 4 arrive at 6 and start at once.
Example 3
arrivals = [0,0,4]durations = [4,4,2]limit = 2return = [[0,4],[0,4],[4,6]]Jobs 0 and 1 end at 4. A job that ends at 4 frees its slot at 4, so job 2, which arrives at 4, starts at 4.
Constraints
1 <= arrivals.length <= 1000anddurations.length == arrivals.length0 <= arrivals[i] <= 10^6, andarrivalsis sorted in non-decreasing order1 <= durations[i] <= 10^61 <= limit <= 1000
More Rippling Phone Screen questions
- Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dependency-Aware Task SchedulerPHONE SCREEN · Seen Nov 2026
- Delivery Cost Tracker with Effective-Dated RatesPHONE SCREEN · Seen Nov 2026
- Camel CardsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best and Worst Camel Card CompletionsPHONE SCREEN · Seen Nov 2026
- Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- In-Memory Priority Task SchedulerPHONE SCREEN · Seen Oct 2026
- Limit an Organization Tree's HeightPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Median Across N Sorted ArraysPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- ID Allocator with Assign and ReleaseSnap Inc. · PHONE SCREENSnap Inc. interview questions
- Last Robot ScoreNVIDIA · PHONE SCREENNVIDIA interview questions
- Allocate and Reuse Server Names by TypeStripe · PHONE SCREENStripe interview questions
- Alert Engine with a Repeat IntervalChronosphere · PHONE SCREEN
- Finish Time of Fixed-Length CPU TasksGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Timer Job Scheduler Wake-upsRedNote · PHONE SCREEN
- Shortest-Job-First Process SchedulingByteDance · PHONE SCREENByteDance interview questions
- Key-Value Store on a Block DeviceLambda · PHONE SCREEN
- 15-Minute Chat Session TrackerOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Lazy K-Way Union IteratorMongoDB · PHONE SCREENMongoDB interview questions