Investable Periods
Reported by candidates from Mathwork's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
You've got a Mathwork OA coming in the next 48 hours and it's asking about investable periods. This is a window-sliding or interval-merging problem dressed up in finance language. The trick is spotting when you can collapse overlapping or adjacent time windows into single stretches where some condition holds true. Candidates often overcomplicate it by trying to track individual stocks or prices when the real move is simpler: identify the boundaries, merge where you can, count what remains. StealthCoder will catch the pattern if you blank on the merging logic.
Pattern and pitfall
Investable periods typically means finding contiguous stretches of time where some financial condition is met, or merging overlapping windows where investment is possible. The core pattern is interval merging or sliding window, sometimes with a greedy pass to collapse boundaries. You're likely given start-end pairs or a time series, and you need to either merge overlapping ranges, filter by some criteria, or count valid windows. The catch: candidates confuse 'overlap' logic (where does interval A intersect B) with 'merge' logic (when do you fuse them into one). The solution almost always involves sorting by start time, then iterating once and deciding at each step whether to extend the current window or start a new one. StealthCoder will have the merge template locked in case you hesitate on the boundary conditions.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Investable Periods 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 merge intervals. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Mathwork's OA.
Mathwork 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.
Investable Periods FAQ
Is this a sorting problem or dynamic programming?+
Sorting first, then greedy iteration. Sort intervals by start time, then single pass to merge. Not DP. The greedy choice is local (merge if overlapping) and that's globally optimal for this type of problem.
What's the most common mistake on investable periods?+
Confusing 'adjacent' with 'overlapping'. If period A ends at day 5 and period B starts at day 5, do they merge? Check the problem wording carefully. Also, not sorting before merging will tank your solution.
How do I handle edge cases like zero periods or single period?+
Test empty input first. Single period returns that period. Multiple non-overlapping periods return all of them. The algorithm still works; it's just fewer merges. Don't special-case unless forced.
Should I use a heap or just a sorted array?+
Sorted array is cleaner here. Sort once, iterate once. A heap adds complexity without benefit. Save heap for problems where you're dynamically inserting intervals or finding mins/maxes.
What if the problem adds a 'minimum duration' filter?+
After merging, filter merged intervals by duration before returning. Merge first, filter second. Don't try to filter during the merge; you'll lose track of what's valid.