The "Count Subarrays of Length Three With a Condition interview question" is a fixed-size window problem. You are given an array of integers and need to count how many contiguous subarrays of length exactly three satisfy a specific condition (for example, the first and third elements are equal, or the sum matches a target). This is an introductory array challenge focused on window traversal.
Microsoft and Meta ask the "Count Subarrays of Length Three coding problem" to evaluate a candidate's basic loop control and array indexing. It’s a warm-up question that ensures you can handle boundaries (not going out of index) and correctly extract segments of a specific length. It checks for clean, concise coding in the "Array interview pattern" category.
This problem follows the Sliding Window (Fixed Size) pattern.
arr[i] == arr[i+2]).Array: [1, 2, 1, 3, 1], Condition: arr[i] == arr[i+2]
[1, 2, 1]. . (Count = 1)[2, 1, 3]. .[1, 3, 1]. . (Count = 2)
Result: 2.When a problem specifies a small fixed length (like 3), a simple loop with direct index access is often better and faster to write than a general-purpose sliding window implementation. Keep it simple and focus on boundary correctness.