Count Intervals After Each Added Number
Updated
Problem statement
Given an integer array nums, add its values one at a time to a set that starts empty, and return an array whose i-th value is the number of intervals in the set right after nums[i] is added.
The values in the set always group into separate intervals of consecutive integers. Adding a value next to an interval extends it, and a value that fills the gap between two intervals merges them into one. Adding a value that is already in the set changes nothing.
Note: an interval is a maximal run of consecutive integers a, a + 1, ..., b that are all in the set.
Function
intervalCountAfterEachAdd(nums: int[]) → int[]Examples
Example 1
nums = [5,7,6,1,2,9]return = [1,2,1,2,2,3]After 5 and 7 there are two intervals. Adding 6 merges them into [5, 7]. Then 1 starts a new interval, 2 extends it to [1, 2], and 9 starts a third.
Example 2
nums = [3,3,4,2]return = [1,1,1,1]The second 3 is already in the set. Then 4 and 2 each extend the same interval, ending at [2, 4].
Example 3
nums = [-1000000000,1000000000]return = [1,2]The two values are far apart, so each one is its own interval.
Constraints
1 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9
More Hudson River Trading Online Assessment questions
- Count Palindrome Addition ChoicesOA · Seen Nov 2026
- Count Multiples of Three with Two SevensOA · Seen Oct 2026
- Recursively Expanded Shell Command CountsOA · Seen Oct 2026
- Rotate Matrix Regions Around the DiagonalsOA · Seen Oct 2026
- Maximum Diamond SumOA · Seen Oct 2026
- Smallest Most-Lit Street PositionOA · Seen Oct 2026
- Rearrange String by Alternating EndsOA · Seen Sep 2026
- Most Items Within Budget from a Start IndexOA · Seen Sep 2026
- Count Fancy NumbersOA · Seen Sep 2026
- Count Pieces After Othello MovesOA · Seen Sep 2026
Similar questions at other companies
- Minimum Contiguous ReplacementsAmazon · OA
- Longest Contiguous Houses After Each BuildZipRecruiter · OAZipRecruiter interview questions
- Neighboring House GroupsAtlassian · OAAtlassian interview questions
- Count Distinct Word MeaningsDuolingo · OADuolingo interview questions
- Disaster RecoveryHeadlands Technologies · OAHeadlands Technologies interview questions
- Worker Management (Promotion Track), Part 4: Full-Shift Double PayAirbnb · OAAirbnb interview questions
- Worker Management, Part 4: Double-Paid IntervalsAnthropic · OAAnthropic interview questions
- Peak Concurrent Calls by UTC DayHubSpot · OA
- Resolve Overlapping Price RangesSquarepoint Capital · OASquarepoint Capital interview questions
- Numbers Station Message DecoderOptiver · OAOptiver interview questions