Back to questions
At your big-tech company, teams are fighting tooth and nail to train their AI models on the company’s shiny new NVIDIA GPUs. It’s like the Hunger Games but with less archery and more coding.
The GPUs are in such high demand that some teams “accidentally” (totally on purpose) overlap their training sessions. It’s chaos. The GPU cluster is booked for days on end, and now everyone’s looking for gaps to squeeze in their own usage.
Unlike those greedy GPU hogs, your team has been given strict instructions: you can only run your training on days when nobody else is using the GPUs. Your VP made it crystal clear that overlapping sessions are not an option.
You’re given two things:
Your mission is to figure out how many days the GPUs are sitting idle so that your team can swoop in and get some guilt-free training time.
Input: ,
Output: 2
Explanation:
Day 6 and day 9 are the only days when the GPUs are free.
Input: ,
Output: 0
Explanation:
The GPUs didn’t get a single day off. Your team will just have to wait patiently until the chaos subsides.
If training sessions never overlapped, we could simply calculate how many days each session would take by using the start and end date of each session, summing this, and then subtracting the total from the given number of days to determine how many days the GPUs are idle.
Here’s the process we would have followed (if sessions did not overlap):
However, sessions can overlap. This causes issues because overlapping intervals could lead to double-counting of GPU usage, which would result in inaccurate idle day calculations. To solve this, we need to merge overlapping intervals and then calculate the total days the GPUs are being used.
Here’s the updated process:
Here is the code:
The only reason we collected the merged sessions earlier was to count the total number of days the sessions are scheduled. Instead, we can calculate the total days directly while merging intervals, avoiding extra space.
Here’s the plan:
Here’s the code: