Magicsheet logo

Monotonic Array

Easy
86.4%
Updated 6/1/2025

Monotonic Array

What is this problem about?

The Monotonic Array problem asks you to determine whether a given array is either entirely non-increasing or entirely non-decreasing. An array is monotonic if it moves in only one direction. This Monotonic Array coding problem is a simple yet clean exercise in single-pass array reasoning.

Why is this asked in interviews?

Microsoft, Meta, Amazon, Google, Bloomberg, and Adobe ask this as a warm-up problem to evaluate code clarity and efficiency. It tests whether candidates can write a clean O(n) solution without unnecessarily complex logic. The array interview pattern is demonstrated here with precision.

Algorithmic pattern used

Single pass with two flags. Track isIncreasing and isDecreasing as booleans. For each adjacent pair, if arr[i] > arr[i+1], set isDecreasing = false. If arr[i] < arr[i+1], set isIncreasing = false. If both become false, return false immediately. Return true at the end.

Example explanation

Array: [1, 3, 5, 5, 7].

  • All non-decreasing → isDecreasing becomes false.
  • isIncreasing remains true.
  • Result: true (monotone increasing).

Array: [5, 3, 4, 2].

  • 3 < 5: isIncreasing = false. 4 > 3: isDecreasing = false. Both false → false.

Common mistakes candidates make

  • Using two separate passes (one for increasing, one for decreasing) when one pass suffices.
  • Forgetting that equal consecutive elements are allowed (non-strict monotonicity).
  • Early termination logic errors — returning false when only one flag is false.
  • Not handling single-element or empty arrays (always monotonic).

Interview preparation tip

Easy problems like Monotonic Array are tests of clean, minimal code. The two-flag single-pass approach is optimal and elegant. Avoid overcomplicating with sorting or multiple passes. In interviews, solving easy problems quickly and with clean code shows confidence and frees time for harder follow-ups. Practice writing this solution in under 5 minutes from scratch.

Similar Questions