Prorated Monthly Subscription Charge
Reported by candidates from Clipboard Health's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Clipboard Health prorated subscription charge question showed up in July 2024 reports, and it looks like a five-minute warm-up until the zero handling bites you. It's a plain array and math problem: count active days per user, multiply by price, divide by days in the month. Sounds easy. The trap is the 0 sentinels and the rounding rule, and both quietly wreck a naive solution. If you've got an OA coming, know exactly where the edges are before you start typing. StealthCoder sits invisibly on your screen as a safety net if you blank on the details mid-assessment.
The problem
Compute a customer's monthly subscription charge in cents. The subscription costs monthlyPriceCents per active user for a full month of daysInMonth days. User i is active from activatedDay[i] through deactivatedDay[i], inclusive. An activation day of 0 means the user was already active before this month; a deactivation day of 0 means the user remains active after this month. Sum all prorated user charges, then round the final total to the nearest cent, with an exact half cent rounded up. Function monthlyCharge(daysInMonth: int, monthlyPriceCents: int, activatedDay: int[], deactivatedDay: int[]) → int Examples Example 1 daysInMonth = 30 monthlyPriceCents = 3000 activatedDay = [1,16] deactivatedDay = [30,30] return = 4500 One user is active all month and the other for 15 of 30 days. Constraints 28 <= daysInMonth <= 31. 0 <= monthlyPriceCents <= 10^7. activatedDay.length == deactivatedDay.length <= 100000. Each nonzero day lies in the month, and each user's active interval is valid.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to treat 0 as a sentinel before you do any arithmetic. If activatedDay is 0, set start to 1. If deactivatedDay is 0, set end to daysInMonth. Then active days are end - start + 1, inclusive on both ends. Off-by-one is the first pitfall. The second is rounding per user. Don't. The problem says sum all prorated charges, then round once. So add up total active days across all users, multiply by monthlyPriceCents, and you have one numerator over daysInMonth. Round half up with integer math: (numerator * 2 + daysInMonth) / (2 * daysInMonth), using floor division. Never use floats here. With 100000 users and a price up to 10^7, the numerator can reach roughly 3 * 10^16, which overflows 32-bit ints, so use 64-bit. If you freeze on the rounding formula during the live OA, StealthCoder is the hedge that gives you the integer version fast.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Prorated Monthly Subscription Charge cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderYou've seen the question.
Make sure you actually pass Clipboard Health's OA.
Clipboard Health reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Prorated Monthly Subscription Charge FAQ
How hard is the Clipboard Health prorated charge problem really?+
Easy on algorithm, tricky on details. It's a single pass over the arrays, O(n). The difficulty is the 0 sentinels, inclusive day counting, and rounding once at the end. Most failed attempts come from rounding per user or using floating point.
What's the trick to the rounding rule?+
Keep everything as integers. Sum active days across all users, multiply by monthlyPriceCents, then round half up with (num * 2 + d) // (2 * d), where d is daysInMonth. That avoids float error and handles the exact half cent case correctly.
How should I handle activation or deactivation day of 0?+
Normalize first. Activation 0 means active since before the month, so treat the start as day 1. Deactivation 0 means active after the month, so treat the end as daysInMonth. Then days = end - start + 1. Do this before any math.
Do I need 64-bit integers for this one?+
Yes, in languages with fixed ints. Up to 100000 users times 31 days times a price of 10^7 gives about 3.1 * 10^13, and doubling it for rounding stays safe in 64-bit but overflows 32-bit. Python handles it automatically.
How do I prepare for this in 48 hours?+
Write the function once from scratch and test the edges: both sentinels as 0, a single-day user, a price of 0, and an exact half cent case. Check that you round once at the end. That covers nearly every way this problem fails.