Add Course Batches All or Nothing
Updated
Problem statement
A school keeps a schedule of courses. A course may have prerequisites: other courses that must also be in the schedule. You are given the current schedule existing and a list of batches of new courses batches. Go through the batches in order and decide for each one whether it can be added.
Courses are written as rows. A row [c, p1, p2, ...] describes course c with prerequisites p1, p2, ..., and a row [c] is a course with no prerequisites. existing is a list of rows, and each batch batches[j] is a list of rows too.
Examples
Example 1
existing = [[0],[1,0],[2,1]]batches = [[[3,2],[4,3,0]],[[5,6],[6,5]],[[5,4],[6,7]],[[5,4],[6,5]]]return = [true,false,false,true]The schedule starts with courses 0, 1 and 2. Batch 0: course 3 needs 2, which is in the schedule, and course 4 needs 3 (in the same batch) and 0 (in the schedule), so the batch is added. Batch 1: course 5 needs 6 and course 6 needs 5, so neither can come first, and the batch is rejected. Batch 2: course 6 needs 7, which is neither in the schedule nor in the batch, so the batch is rejected, and course 5 is not added either, even though its prerequisite 4 is in the schedule. Batch 3: course 5 needs 4, in the schedule since batch 0, and course 6 needs 5, in the same batch, so the batch is added. The schedule ends with courses 0 to 6.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 2 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.
More Amazon Onsite Interview questions
- Priority Ad Rotation with Show LimitsONSITE INTERVIEW · Seen Dec 2026
- Next Greater Element IONSITE INTERVIEW · Seen Dec 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Optimal Account BalancingONSITE INTERVIEW · Seen Dec 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Flood FillONSITE INTERVIEW · Seen Nov 2026
- Merge k Sorted ListsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Merge Event PreferencesAirbnb · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
- Package Install OrderVerily · ONSITE INTERVIEW
- Topological Sort with Secondary OrderingRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Evaluate Formulas with Cycle DetectionInstacart · PHONE SCREEN · ONSITE INTERVIEWInstacart interview questions
- Task Build Order and File-Change RerunsVanta · PHONE SCREEN
- Validate CSV References and Field DependenciesStripe · PHONE SCREENStripe interview questions
- Recipes You Can PrepareIndeed · PHONE SCREENIndeed interview questions
- Data Pipeline Steps with Search and Run OrderRippling · PHONE SCREENRippling interview questions
- Propagate Spreadsheet Cell UpdatesOpenAI · PHONE SCREENOpenAI interview questions
- Lexicographically Smallest Topological OrderCoinbase · PHONE SCREENCoinbase interview questions
Amazon interview experiences
- Amazon Experienced SDE Onsite Questions: 1 Experience
- Amazon SDE Onsite Interview Questions: 9 Experiences
- Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
- Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
- Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
- Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)