← Meta Interview Insights

Meta·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Meta SWE coding round with a math-heavy array problem. Nothing crazy on the surface but the edge cases with negatives and zeros kept me second-guessing my parity logic the whole time.

Questions Asked (1)

Q1

Given an integer array, count all pairs of indices (i, j) where i < j, the product of the two elements is even, and the distance between the indices is odd.

Algorithms & Data Structures
Author's notes

My first instinct was brute force and I actually started coding it before catching myself.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, restate the problem and clarify constraints (e.g., array size, element range). Then, derive an O(n) solution by counting even and odd elements at even and odd indices, since the conditions depend only on parity. Finally, compute the valid pairs using combinatorics and verify with a brute-force check on small examples.

Pro tip: Mention that you can solve it in O(n) time and O(1) space by counting parities, which is optimal. Also, proactively discuss edge cases like empty arrays or all odd numbers to show thoroughness.

1. Clarify and Restate

Confirm the problem: count pairs (i, j) with i < j, (arr[i] * arr[j]) even, and (j - i) odd. Ask about input size and element range to determine if O(n^2) is acceptable.

2. Analyze Conditions

Note that product is even if at least one element is even. Distance odd means indices have opposite parity (one even, one odd). So valid pairs are those where one index is even and the other odd, and at least one element is even.

3. Count Parities

Traverse the array once, counting the number of even and odd elements at even indices and at odd indices. Let E_even, O_even, E_odd, O_odd be these counts.

4. Compute Valid Pairs

Total pairs with opposite index parity = (E_even + O_even) * (E_odd + O_odd). Subtract pairs where both elements are odd: O_even * O_odd. Result = (E_even + O_even)*(E_odd + O_odd) - O_even*O_odd.

5. Verify and Optimize

Test with small examples (e.g., [1,2,3,4]) to ensure correctness. Discuss time O(n) and space O(1), and mention that this is optimal since we must read the array.

Key Points to Mention

  • Product even condition: at least one element is even.
  • Distance odd condition: indices have opposite parity (i even, j odd or vice versa).
  • Counting even/odd elements at even/odd indices in one pass.
  • Combinatorial formula: total opposite-index pairs minus pairs where both are odd.
  • Time complexity O(n) and space O(1).
  • Edge cases: empty array, single element, all odd, all even.

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