The brute force is obvious and they'd probably let you code it, but the real question is whether you see the prefix sum angle.
Preprocess the array to create a prefix array where each element indicates whether the adjacent pair ending at that index alternates (i.e., has different parity). Then, for each query [l, r], check if the sum of alternating indicators from l+1 to r equals r - l, which means every adjacent pair in the subarray alternates. This yields O(n) preprocessing and O(1) per query.
Pro tip: Clarify that the alternating condition is about parity (odd/even), not value, and mention that the prefix sum approach works because the condition is monotonic and can be aggregated. Also, handle edge cases like l == r (single element) which trivially alternates.
Clarify that a subarray alternates if for every i from l to r-1, arr[i] and arr[i+1] have different parity (one odd, one even). Confirm that a single-element subarray always satisfies the condition.
Create an array alt of size n-1 where alt[i] = 1 if arr[i] and arr[i+1] have different parity, else 0. Then build a prefix sum array pref where pref[i] = sum of alt[0..i-1] (with pref[0]=0).
For a query [l, r], if l == r, answer true. Otherwise, compute the number of alternating adjacent pairs in the subarray as pref[r] - pref[l] (using 0-indexed arrays and adjusting indices appropriately). If this count equals r - l, then every adjacent pair alternates, so answer true; else false.
Preprocessing takes O(n) time and O(n) space. Each query is answered in O(1) time. This meets the required constraints.
Walk through a small example, such as arr = [1,2,3,4] and queries [0,3] (alternates? 1-2 yes, 2-3 yes, 3-4 yes => true) and [0,2] (1-2 yes, 2-3 yes => true). Also test a non-alternating case like arr = [1,3,2] and query [0,2] (1-3 no => false).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.