Detect runs and answer suffix queries
Company: Anthropic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in array processing, run-length detection, algorithmic complexity analysis, and the use of range/suffix data structures within the Coding & Algorithms domain.
Part 1: Maximum Length of a Consecutive Equal Run
Constraints
- `0 <= len(nums) <= 2 * 10^5`
- `-10^9 <= nums[i] <= 10^9`
- Only equality comparisons are needed
Examples
Input: ([1, 1, 2, 2, 2, 3],)
Expected Output: 3
Explanation: The longest run is `[2, 2, 2]`, which has length 3.
Input: ([5],)
Expected Output: 1
Explanation: A single element forms a run of length 1.
Hints
- Scan from left to right while tracking the current run length and the best run seen so far.
- When the value changes, reset the current run to 1. Do not forget to account for the final run.
Part 2: Does Any Run Reach Length N?
Constraints
- `0 <= len(nums) <= 2 * 10^5`
- `1 <= n <= 2 * 10^5`
- `-10^9 <= nums[i] <= 10^9`
Examples
Input: ([1, 1, 2, 2, 2, 3], 3)
Expected Output: True
Explanation: There is a run `[2, 2, 2]` of length 3.
Input: ([1, 2, 2, 3], 3)
Expected Output: False
Explanation: The longest run has length 2, which is less than 3.
Hints
- Use the same idea as Part 1, but you can stop early as soon as the current run reaches `n`.
- Handle the small boundary case `n == 1` separately.
Part 3: Suffix Queries for Runs of Length at Least N
Constraints
- `0 <= len(nums) <= 2 * 10^5`
- `1 <= n <= 2 * 10^5`
- `0 <= len(queries) <= 2 * 10^5`
- If `nums` is non-empty, each query index is typically in the range `0 <= queries[i] < len(nums)`
- `-10^9 <= nums[i] <= 10^9`
Examples
Input: ([1, 1, 2, 2, 2, 3, 3], 3, [0, 1, 2, 3, 4, 5])
Expected Output: [True, True, True, False, False, False]
Explanation: Only suffixes starting at indices 0, 1, and 2 still contain a run of length at least 3.
Input: ([5, 5, 5, 5], 3, [0, 1, 2])
Expected Output: [True, True, False]
Explanation: The suffix at index 1 is `[5, 5, 5]`, which still has a run of length 3.
Hints
- Compute `start_len[i]`: the length of the equal-valued run starting at index `i`. This is easiest to build from right to left.
- Then compute `suffix_has[i]`: whether any run of length at least `n` starts at or after `i`. Each query becomes a constant-time lookup.