Syllabus Lesson 36 of 141 · Ask a different question
Ask a different question

Change the unit: billing by the minute

Why it matters Billing rules are written in the unit the customer thinks in, usually minutes or days. Code written in that unit does work proportional to how long things ran, not to how many things happened.

Your company rents build machines. The licence for the build tool covers a number of machines running at once; every minute in which more machines run than the licence allows is billed as overage. Finance wants the overage minutes for the month.

Each session is one machine from a start time up to, not including, an end time, both on whole minutes. Sessions often last days.

The data

from datetime import datetime
sessions = [("vm1", datetime(2026, 3, 2, 9, 0), datetime(2026, 3, 2, 9, 5)),
            ("vm2", datetime(2026, 3, 2, 9, 2), datetime(2026, 3, 2, 9, 10)),
            ("vm3", datetime(2026, 3, 2, 9, 3), datetime(2026, 3, 2, 9, 4))]
minutes_over_limit(sessions, 1)   # -> 3  (09:02, 09:03 and 09:04)
Your turn

Write minutes_over_limit(sessions, limit). sessions is a list of (machine_id, start, end) with datetime values on whole minutes; a session runs during start <= t < end. Return the number of whole minutes in which more than limit sessions are running. Budget: a month with 600 sessions (up to six days each) must take at most 1,000,000 steps of your own code.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.