Magicsheet logo

Special Array I

Easy
100%
Updated 6/1/2025

Special Array I

What is this problem about?

The Special Array I interview question introduces a simple yet fundamental concept of array parity. An array is considered "special" if every pair of adjacent elements has different parity—meaning one is even and the other is odd. The task is to write a function that checks if a given array of integers satisfies this condition. It is an introductory level problem that focuses on basic array traversal and conditional logic.

Why is this asked in interviews?

While it might seem basic, this Special Array I coding problem is frequently used by companies like Microsoft and Amazon as a "warm-up" question. It allows interviewers to assess a candidate's coding speed, attention to detail, and ability to handle edge cases (like an array with only one element). It's a great way to verify that a candidate can write clean, bug-free code for a straightforward Array interview pattern.

Algorithmic pattern used

This problem uses a simple Linear Scan or Array Traversal. You only need to look at each element once, comparing it with its immediate neighbor. The logic revolves around the modulo operator (% 2) to determine parity. If you ever find two neighbors that are both even or both odd, you can immediately conclude the array is not special.

Example explanation

Consider the array [1, 2, 3, 4, 5].

  1. Look at 1 (odd) and 2 (even). They are different. Good.
  2. Look at 2 (even) and 3 (odd). They are different. Good.
  3. Look at 3 (odd) and 4 (even). They are different. Good.
  4. Look at 4 (even) and 5 (odd). They are different. Good. Result: The array is Special.

Now consider [1, 2, 2, 4].

  1. Look at 1 (odd) and 2 (even). Good.
  2. Look at 2 (even) and 2 (even). They are the same! Result: The array is NOT Special.

Common mistakes candidates make

  • Index Out of Bounds: Trying to check array[i] and array[i+1] but letting the loop run all the way to the last index, causing a crash.
  • Empty Array/Single Element: Not clarifying how the function should behave for an array with zero or one element (usually, a single element array is considered special).
  • Early Return Logic: Returning true too early inside the loop before checking all pairs.
  • Inefficient Logic: Overcomplicating the parity check instead of using a simple (a % 2) != (b % 2).

Interview preparation tip

For "Check" type problems, always look for the first violation. As soon as you find a pair that fails the condition, return false. This is more efficient than checking every single pair if you already know the answer. Also, always double-check your loop boundaries to ensure you don't overshoot the array size.

Similar Questions