Detect n-length consecutive sequences
Company: Anthropic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's competency in algorithm design and data-structure reasoning for detecting n-length consecutive integer sequences, encompassing handling of duplicates, negative values, and dynamic insertions and deletions.
Part 1: Detect a consecutive run in an unsorted distinct array
Constraints
- 0 <= len(nums) <= 100000
- 0 <= nums[i] <= 10^9
- All values in nums are distinct
- 1 <= n <= 100000
Examples
Input: ([8, 1, 4, 3, 2], 4)
Expected Output: [1, 2, 3, 4]
Explanation: The numbers 1, 2, 3, and 4 are all present.
Input: ([10, 6, 7, 8, 20], 3)
Expected Output: [6, 7, 8]
Explanation: The array contains one run of length 3 starting at 6.
Hints
- Sorting the numbers can turn the problem into finding a long enough consecutive streak.
- Because all values are distinct in this version, you only need to compare adjacent sorted elements.
Part 2: Detect a consecutive run with duplicates and negative numbers
Constraints
- 0 <= len(nums) <= 100000
- -10^9 <= nums[i] <= 10^9
- Duplicates may appear in nums
- 1 <= n <= 100000
Examples
Input: ([-2, -1, -1, 0, 4, 5], 3)
Expected Output: [-2, -1, 0]
Explanation: After ignoring duplicates, -2, -1, and 0 form a valid run.
Input: ([3, 3, 2, 1, 10], 3)
Expected Output: [1, 2, 3]
Explanation: The duplicate 3 should count only once.
Hints
- Duplicates should be ignored when deciding whether values are consecutive.
- After removing duplicates, a sorted scan works even when numbers are negative.
Part 3: Find a consecutive run in near-linear time
Constraints
- 0 <= len(nums) <= 200000
- -10^9 <= nums[i] <= 10^9
- Duplicates may appear in nums
- 1 <= n <= 200000
Examples
Input: ([100, 4, 200, 1, 3, 2], 4)
Expected Output: [1, 2, 3, 4]
Explanation: The classic consecutive run 1 through 4 is present.
Input: ([10, 11, 12, 1, 2, 3, 4], 3)
Expected Output: [1, 2, 3]
Explanation: Both 1-3 and 10-12 work, so return the smaller start.
Hints
- A hash set gives O(1) average membership checks.
- Only try to grow a run from values whose predecessor is not present.
Part 4: Maintain consecutive runs in an online stream
Constraints
- 1 <= n <= 100000
- 0 <= len(operations) <= 20000
- -10^9 <= value <= 10^9
- Delete on a value not currently present should be ignored
- The stream is a multiset: a value disappears from the distinct set only when its count drops to 0
Examples
Input: (3, [[1, 5], [1, 1], [1, 2], [1, 3], [3, 0], [1, 10], [1, 11], [1, 12], [3, 0]])
Expected Output: [[1, 2, 3], [1, 2, 3]]
Explanation: After the first four inserts, 1-3 exists. Later 10-12 also exists, but 1-3 has the smaller start.
Input: (3, [[1, -1], [1, 0], [1, 1], [1, 1], [3, 0], [2, 0], [3, 0], [2, 1], [3, 0], [1, 0], [3, 0]])
Expected Output: [[-1, 0, 1], [], [], [-1, 0, 1]]
Explanation: Duplicates are counted, so deleting one 1 does not remove the value 1 entirely.
Hints
- Think in terms of maximal consecutive intervals of currently active distinct values.
- Keep a count for each value so duplicates only affect the structure when a count changes between 0 and 1.