Check Alternating Highs and Lows Around a Circular Array
Capital OneSoftware Engineer · Online AssessmentLast updated
Check strict high-low alternation around a circular array, including equal neighbors, wraparound comparisons, and the two-element case.
An array is connected end to end to form a circle. Determine whether its values strictly alternate between a high point and a low point around the entire circle.
Implement is_alternating_circle(values: int[]) -> bool. Every element must be strictly greater than both of its neighbors or strictly less than both of its neighbors. The first and last elements are neighbors.
Constraints & Assumptions
- There are between 2 and 200,000 elements, each between -1,000,000,000 and 1,000,000,000.
- Equal neighboring values are not allowed in a valid alternating circle.
- For length two, both neighbor positions of an element refer to the other element. Two distinct values therefore form a valid alternating circle.
- Strict comparisons and the length-two convention are explicit practice choices for the reported high/low pattern.
- Return one Boolean; no rotation or rearrangement of the input is allowed.
Examples
[1, 4, 2, 5]returnstrue. Each low value has two higher neighbors and each high value has two lower neighbors, including across the last-to-first boundary.[1, 4, 2]returnsfalse. The value 2 is lower than 4 but higher than 1, so it is neither a high nor a low point.
Hints
Hint 1
Include the closing edge: A linearly alternating prefix does not prove the circle works. Check each value against both neighbors using wraparound indices.
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.