Days Between
Reported by candidates from Optiver's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
You've got a date math problem from Optiver in March 2026. Two dates, three integers each, and you need the days between them. The trap is reaching for built-in date helpers or trying to brute-force a loop over potentially large year ranges. You need to know how to calculate days from a reference point (like year zero) and subtract. StealthCoder will have the leap-year logic ready if your brain stalls under pressure.
The problem
Each date is represented by three integers: year, month, and day. The first date is guaranteed to occur before the second date. Do not use system-provided date objects or built-in date-difference helpers. Compute the answer directly from the date components. Function Description Complete the function daysBetween in the editor below. daysBetween has the following parameters: Returns
Reported by candidates. Source: FastPrep
Pattern and pitfall
The clean solution converts each date to an absolute day count from a reference point, then subtracts. You'll need a helper to count days from year 1 (or year 0) to any given date, accounting for leap years correctly. A year is a leap year if it's divisible by 400, or divisible by 4 but not 100. Then calculate cumulative days up to the given month and day. The math pattern is prefix-sum across years and months. Common pitfall: off-by-one errors in month ranges or forgetting that leap years affect February. StealthCoder anchors you to the formula if you freeze on the leap-year condition.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Days Between 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 StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as number of days between two dates. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Optiver's OA.
Optiver 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.
Days Between FAQ
How do I handle leap years without looking them up?+
A year is a leap year if: (year % 400 == 0) OR (year % 4 == 0 AND year % 100 != 0). That's it. Memorize it. February has 29 days in a leap year, 28 otherwise.
What's the reference point I should use?+
Pick any fixed date, like January 1, year 1 (or year 0). Convert both input dates to their absolute day count from that reference. Then subtract. The choice of reference doesn't matter as long as it's consistent.
Is there a trick to avoid iterating over every year?+
Yes. Use the formula for days elapsed in complete years: (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 * 365 + (year - 1). This avoids looping. Integer division handles the leap-year count automatically.
What's the most common bug in this problem?+
Off-by-one in the day count or miscounting leap years between the two dates. Also, candidates forget that the problem says first date comes before second, so you never need to handle negative results.
How do I prepare in 48 hours if I haven't done date math before?+
Write a helper function that returns days from January 1, year 1 to any date. Test it on a few known dates (like leap years). Then subtract. That's the entire problem. Practice once, you own it.