← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Meta SWE coding round, one algorithmic problem about counting subarrays with alternating parity. Pretty focused session, just the one question but it had some depth to it once you started thinking about edge cases.

Questions Asked (1)

Q1

Given an integer array, count all contiguous subarrays where adjacent elements strictly alternate between even and odd numbers.

Algorithms & Data Structures
Author's notes

The example they give is [1,2,3,7,8,9] where you get 12 total subarrays.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a linear scan to identify maximal alternating subarrays, then count all contiguous subarrays within each. For each maximal alternating segment of length L, the number of valid subarrays is L*(L+1)/2. Sum these counts to get the total.

Pro tip: Clarify edge cases upfront (e.g., empty array, single element) and discuss time/space complexity. Mention that the O(n) solution is optimal and explain why a brute-force O(n^2) approach is inefficient.

1. Understand the problem

Restate the problem: count contiguous subarrays where adjacent elements alternate between even and odd. Confirm with the interviewer that a subarray of length 1 is always valid.

2. Identify maximal alternating segments

Scan the array and find maximal contiguous segments where each adjacent pair alternates in parity. Track the length of the current segment.

3. Count subarrays per segment

For a segment of length L, all contiguous subarrays within it are valid. The count is L*(L+1)/2. Add this to the total.

4. Handle edge cases and complexity

Consider empty array (return 0), single element (return 1). Analyze time complexity O(n) and space O(1).

Key Points to Mention

  • Definition of alternating parity: (a[i] % 2) != (a[i+1] % 2)
  • Maximal alternating segments and how to identify them in one pass
  • Formula for counting subarrays in a segment: L*(L+1)/2
  • Time complexity O(n) and space complexity O(1)
  • Edge cases: empty array, single element, all same parity
  • Comparison with brute-force O(n^2) approach and why linear is better

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.