← Capital One Interview Insights
Took me a minute to realize single-element subarrays always count, which is kind of a free baseline you can build on.
Use a linear scan to track the length of the current alternating parity run, adding that length to a running total at each step. This works because every new element extends all valid subarrays ending at the previous element by one, plus starts a new subarray of length 1.
Pro tip: Clarify that 'strictly alternate' means adjacent elements must have different parity, and confirm whether subarrays of length 1 count (they do). Also mention that this O(n) solution is optimal since you must examine each element at least once.
Confirm that subarrays must be contiguous, length >= 1, and that parity alternation means adjacent elements have different parity (even vs odd).
Recognize that if the current element alternates with the previous one, it extends all valid subarrays ending at the previous element by one, plus forms a new subarray of length 1.
Initialize total = 0 and current_run = 0. For each element, if it alternates with the previous element, increment current_run; otherwise reset current_run to 1. Add current_run to total.
The algorithm runs in O(n) time and O(1) extra space, which is optimal since every element must be examined.
Walk through a small example (e.g., [1,2,3]) to verify the logic and edge cases like single-element arrays or all-even arrays.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.