Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
An array is monotonic if it is either monotone increasing or monotone decreasing.
An array nums is monotone increasing if for all i <= j, nums[i] <= nums[j]. An array nums is monotone decreasing if for all i <= j, nums[i] >= nums[j].
Given an integer array nums, return true if the given array is monotonic, or false otherwise.
Example 1:
Input: nums = [1,2,2,3] Output: true
Example 2:
Input: nums = [6,5,4,4] Output: true
Example 3:
Input: nums = [1,3,2] Output: false
Constraints:
1 <= nums.length <= 105-105 <= nums[i] <= 105Problem summary: An array is monotonic if it is either monotone increasing or monotone decreasing. An array nums is monotone increasing if for all i <= j, nums[i] <= nums[j]. An array nums is monotone decreasing if for all i <= j, nums[i] >= nums[j]. Given an integer array nums, return true if the given array is monotonic, or false otherwise.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,2,2,3]
[6,5,4,4]
[1,3,2]
count-hills-and-valleys-in-an-array)find-the-count-of-monotonic-pairs-i)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #896: Monotonic Array
class Solution {
public boolean isMonotonic(int[] nums) {
boolean asc = false, desc = false;
for (int i = 1; i < nums.length; ++i) {
if (nums[i - 1] < nums[i]) {
asc = true;
} else if (nums[i - 1] > nums[i]) {
desc = true;
}
if (asc && desc) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #896: Monotonic Array
func isMonotonic(nums []int) bool {
asc, desc := false, false
for i, x := range nums[1:] {
if nums[i] < x {
asc = true
} else if nums[i] > x {
desc = true
}
if asc && desc {
return false
}
}
return true
}
# Accepted solution for LeetCode #896: Monotonic Array
class Solution:
def isMonotonic(self, nums: List[int]) -> bool:
asc = all(a <= b for a, b in pairwise(nums))
desc = all(a >= b for a, b in pairwise(nums))
return asc or desc
// Accepted solution for LeetCode #896: Monotonic Array
impl Solution {
pub fn is_monotonic(nums: Vec<i32>) -> bool {
let mut asc = false;
let mut desc = false;
for i in 1..nums.len() {
if nums[i - 1] < nums[i] {
asc = true;
} else if nums[i - 1] > nums[i] {
desc = true;
}
if asc && desc {
return false;
}
}
true
}
}
// Accepted solution for LeetCode #896: Monotonic Array
function isMonotonic(nums: number[]): boolean {
let [asc, desc] = [false, false];
for (let i = 1; i < nums.length; ++i) {
if (nums[i - 1] < nums[i]) {
asc = true;
} else if (nums[i - 1] > nums[i]) {
desc = true;
}
if (asc && desc) {
return false;
}
}
return true;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.