← Bytedance Interview Insights

Bytedance·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Bytedance SWE interview with an array problem that looks easy until you actually have to get the counting right under pressure.

Questions Asked (1)

Q1

Given an integer array, count the total number of contiguous subarrays where adjacent elements strictly alternate between odd and even values. A single element counts as valid.

Algorithms & Data Structures
Author's notes

The O(n) trick here is to keep a running length counter and add it to your total at each position.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and edge cases, then propose an O(n) solution that tracks the length of the current alternating subarray ending at each index. Explain how each new element extends the previous alternating run or starts a new one, and sum these lengths to get the total count.

Pro tip: Mention that this is a classic 'count subarrays with property' problem where maintaining a running length avoids O(n^2) enumeration, and emphasize that you can solve it in one pass with O(1) extra space.

1. Clarify the problem

Confirm that a single element is always valid, and that 'strictly alternate' means odd-even-odd or even-odd-even with no two adjacent elements having the same parity.

2. Define the state

Let 'length' be the number of valid alternating subarrays ending at the current index. Initialize length = 1 for the first element and total = 1.

3. Iterate and update

For each subsequent element, check if its parity differs from the previous element. If so, increment length; otherwise, reset length to 1. Add length to total.

4. Return the total

After processing all elements, return total as the count of valid contiguous subarrays.

5. Analyze complexity

State that the algorithm runs in O(n) time and uses O(1) extra space, which is optimal for this problem.

Key Points to Mention

  • Parity check using modulo 2 or bitwise AND with 1.
  • Dynamic programming / running length approach.
  • Single pass O(n) time and O(1) space.
  • Handling edge cases: empty array, single element, all same parity.
  • Avoiding O(n^2) brute force by counting subarrays ending at each index.
  • Proof of correctness: each valid subarray is counted exactly once when it ends.

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