Detect n-length consecutive sequences
AnthropicSoftware Engineer · Technical ScreenLast updated
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.
Given an unsorted collection of integers and a parameter n > 0, determine whether there exists a set of n distinct integers that form a run of consecutive values (i.e., some x, x+1, ..., x+n-
- regardless of order. If such a run exists, return any one such run; otherwise, return empty. Follow-ups: (a) Handle duplicates and negative numbers; (b) Achieve near-linear time using appropriate data structures; (c) Adapt the design to an online stream supporting insertions and deletions while answering queries about the existence of an n-length consecutive run.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.