← Questions
Range queries: is the subarray parity-alternating
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's first round (coding), last asked Jan 2026. It tests arrays and prefix sums.
Call an array special when every pair of neighbouring elements has different parity: one is odd and the other even. You get an integer array and many queries [from, to] (inclusive indices). For each query, return whether the subarray nums[from..to] is special.
Interface
Unless you agree on a different interface with the interviewer, assume:
is_special(nums: list[int], queries: list[list[int]]) -> list[bool]
Example
nums = [4, 3, 1, 6], queries = [[0, 2], [2, 3]]
-> [False, True] # 3 and 1 are both odd; 1 and 6 differ
nums = [3, 4, 1, 2, 6], queries = [[0, 4], [0, 3], [3, 4]]
-> [False, True, False] # 2 and 6 are both even
Constraints
-
1 <= len(nums) <= 10^5,1 <= len(queries) <= 10^5. - A single element is special.
Clarifying questions
- Are query bounds inclusive?
- Is a one-element subarray special?
- Can values be negative (how does parity work for them in my language)?
- Will the array change between queries?
What a strong answer covers
- The brute force: scan each query's range, O(n · q).
- Precompute "bad" positions
iwherenums[i]andnums[i-1]share parity, as a prefix count. A query is special exactly when there is no bad position in(from, to]:bad[to] - bad[from] == 0. O(n) preprocessing and O(1) per query. - An equivalent: for each index, the start of the longest special run ending there.
- Tests: single-element queries, the whole array, negative numbers.
Follow-ups
- The array receives point updates between queries: a Fenwick tree over bad positions.
- Return the length of the longest special subarray inside each query range.
- Count the special subarrays in the whole array.
Where it's asked
- Google · First round: coding · Last asked Jan 202645 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI
What it tests
arrays · prefix sums
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
