Function customInterval
Given a function fn, a number delay and a number period, return a number id.
customInterval is a function that should execute the provided function fn at intervals based on a linear pattern defined by the formula delay + period * count.
The count in the formula represents the number of times the interval has been executed starting from an initial value of 0.
Function customClearInterval
Given the id. id is the returned value from the function customInterval.
customClearInterval should stop executing provided function fn at intervals.
Note: The setTimeout and setInterval functions in Node.js return an object, not a number.
Example 1:
Input: delay = 50, period = 20, cancelTime = 225
Output: [50,120,210]
Explanation:
const t = performance.now()
const result = []
const fn = () => {
result.push(Math.floor(performance.now() - t))
}
const id = customInterval(fn, delay, period)
setTimeout(() => {
customClearInterval(id)
}, 225)
50 + 20 * 0 = 50 // 50ms - 1st function call
50 + 20 * 1 = 70 // 50ms + 70ms = 120ms - 2nd function call
50 + 20 * 2 = 90 // 50ms + 70ms + 90ms = 210ms - 3rd function call
Example 2:
Input: delay = 20, period = 20, cancelTime = 150 Output: [20,60,120] Explanation: 20 + 20 * 0 = 20 // 20ms - 1st function call 20 + 20 * 1 = 40 // 20ms + 40ms = 60ms - 2nd function call 20 + 20 * 2 = 60 // 20ms + 40ms + 60ms = 120ms - 3rd function call
Example 3:
Input: delay = 100, period = 200, cancelTime = 500 Output: [100,400] Explanation: 100 + 200 * 0 = 100 // 100ms - 1st function call 100 + 200 * 1 = 300 // 100ms + 300ms = 400ms - 2nd function call
Constraints:
20 <= delay, period <= 25020 <= cancelTime <= 1000Problem Overview: You’re given a collection of intervals and a custom interval. The goal is to determine how this interval fits into the existing set—typically by inserting it and merging overlaps so the final result remains a valid list of non‑overlapping intervals.
Approach 1: Brute Force Overlap Checking (O(n^2) time, O(n) space)
Start by adding the custom interval to the list of existing intervals. Then repeatedly scan the array to detect overlapping pairs. Whenever two intervals overlap, merge them into a single interval and restart the scan. This method relies on repeated comparisons between interval boundaries (start and end). While simple to reason about, the nested scanning leads to quadratic time complexity when many intervals overlap.
Approach 2: Sort and Merge Intervals (O(n log n) time, O(n) space)
Append the custom interval to the interval list and sort all intervals by their starting value. Then iterate through the sorted list and maintain a result array. For each interval, compare it with the last merged interval in the result. If the current interval overlaps (current.start <= last.end), update the end boundary using Math.max. Otherwise, push the interval as a new segment. Sorting ensures that overlapping intervals appear next to each other, allowing a single linear pass to merge them efficiently. This pattern frequently appears in interval problems and scheduling tasks.
Approach 3: Sweep Line Processing (O(n log n) time, O(n) space)
Convert each interval boundary into events: a start event and an end event. Include the custom interval in this event list. Sort events by position and process them sequentially while tracking the number of active intervals. When the active count moves from zero to one, a new merged interval begins; when it returns to zero, the interval ends. This technique—commonly called a sweep line—generalizes well for more complex interval counting or overlap queries.
Recommended for interviews: The sort‑and‑merge approach is the standard expectation. It demonstrates understanding of interval ordering, boundary comparisons, and linear merging. Brute force shows baseline reasoning, but the optimal O(n log n) solution using sorting is what interviewers typically look for in problems involving arrays and interval manipulation.
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Overlap Checking | O(n^2) | O(n) | Small datasets or when demonstrating basic overlap logic |
| Sort and Merge Intervals | O(n log n) | O(n) | General case for inserting or merging intervals |
| Sweep Line Technique | O(n log n) | O(n) | Useful when tracking overlap counts or handling many interval events |
How many LeetCode problems should you solve? #leetcode #techinterview #developer #softwareengineer • CrioDo • 304,684 views views
Watch 9 more video solutions →Practice Custom Interval with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor