Check whether an integer array is monotonic (entirely non-decreasing or non-increasing)

Quick Overview

Decide whether an integer array is monotonic, meaning entirely non-decreasing or entirely non-increasing, with equal neighbors allowed. It tests a single linear pass over adjacent pairs, the reasoning for why neighbor checks are enough, and handling of repeated values.

Check whether an integer array is monotonic (entirely non-decreasing or non-increasing)

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of integers `nums`, return `True` if the array is monotonic (either monotone increasing or monotone decreasing), and `False` otherwise. An array is monotone increasing if `nums[i] <= nums[j]` for all `i <= j`. An array is monotone decreasing if `nums[i] >= nums[j]` for all `i <= j`. ### Function Signature ```python def is_monotonic(nums: list[int]) -> bool: ``` ### Rules - Equal neighboring values are allowed in both directions, so a constant array is monotonic. - An array with one element is monotonic. ### Constraints - `1 <= len(nums) <= 10^5` - `-10^5 <= nums[i] <= 10^5` ### Examples ```text Input: nums = [1, 2, 2, 3] Output: True ``` Each element is greater than or equal to the one before it, so the array is monotone increasing. The repeated 2 does not break that. ```text Input: nums = [6, 5, 4, 4] Output: True ``` Each element is less than or equal to the one before it, so the array is monotone decreasing. ```text Input: nums = [1, 3, 2] Output: False ``` The step from 1 to 3 goes up, so the array is not monotone decreasing, and the step from 3 to 2 goes down, so it is not monotone increasing either.

Overview: Decide whether an integer array is monotonic, meaning entirely non-decreasing or entirely non-increasing, with equal neighbors allowed. It tests a single linear pass over adjacent pairs, the reasoning for why neighbor checks are enough, and handling of repeated values.

Meta logo
Meta
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array of integers nums, return True if the array is monotonic (either monotone increasing or monotone decreasing), and False otherwise.

An array is monotone increasing if nums[i] <= nums[j] for all i <= j. An array is monotone decreasing if nums[i] >= nums[j] for all i <= j.

Function Signature

def is_monotonic(nums: list[int]) -> bool:

Rules

  • Equal neighboring values are allowed in both directions, so a constant array is monotonic.
  • An array with one element is monotonic.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^5 <= nums[i] <= 10^5

Examples

Input: nums = [1, 2, 2, 3]
Output: True

Each element is greater than or equal to the one before it, so the array is monotone increasing. The repeated 2 does not break that.

Input: nums = [6, 5, 4, 4]
Output: True

Each element is less than or equal to the one before it, so the array is monotone decreasing.

Input: nums = [1, 3, 2]
Output: False

The step from 1 to 3 goes up, so the array is not monotone decreasing, and the step from 3 to 2 goes down, so it is not monotone increasing either.