Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Compare each `nums[i]` with `nums[i-1]`; return false on the first inversion. If none, return true. Empty and single-element arrays are sorted by definition.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you exploit transitivity (adjacent checks) and pick the right strictness for duplicates.
Where candidates slip
The `>=` bug that fails on equal neighbours, or an out-of-bounds read.
How to stand out
State why adjacent checks suffice (transitivity) and handle the 0/1-element conventions.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Adjacent-pair check
1Hint 1Explore hintHide hint
Do you need to compare every pair, or just neighbours?
2Hint 2Explore hintHide hint
Return false on the first place where a[i-1] > a[i].
3Hint 3Explore hintHide hint
Use `>` (not `>=`) so equal neighbours stay valid for non-decreasing.
A · Core idea
An array is sorted in non-decreasing order exactly when no adjacent pair decreases. Scan neighbours; return false on the first inversion, true if you reach the end.
Why checking neighbours is enough. Order is transitive: if a[0] ≤ a[1] ≤ a[2] ≤ … then a[0] ≤ a[2] follows for free. So local order across every adjacent pair guarantees global order — you never need to compare non-adjacent elements.
Why `>` and not `>=`. Non-decreasing allows equal neighbours (e.g. [1, 2, 2, 3]). Rejecting on a[i-1] >= a[i] would wrongly flag arrays with duplicates. Use strict > so equals pass.
Empty and single-element arrays. They have no adjacent pairs, so the loop never finds an inversion and returns true — which matches the convention that they're trivially sorted. Start the loop at index 1 to avoid reading a[-1]/out of bounds.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [1, 3, 2]i=1
1 ≤ 3? yes
okfalseC · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(n) time, O(1) space — one comparison per adjacent pair (n−1 of them); can early-exit on the first inversion.
E · Edge cases and pitfalls5nums = [1, 2, 2, 3] — Equal neighbours allowed → true (uses `>`, not `>=`).Explore edge casesHide edge cases
Edge cases handled
nums = [1, 2, 2, 3]Equal neighbours allowed → true (uses `>`, not `>=`).nums = [5]No pairs → true.nums = []Vacuously sorted → true.
Pitfalls
- Using `>=` and rejecting valid arrays with duplicates.
- Starting at i = 0 and reading nums[-1] / index −1.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic boolean isSorted(int[] nums) { for (int i = 1; i < nums.length; i++) { if (nums[i - 1] > nums[i]) return false; } return true;Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1Using >= for non-decreasing
It rejects valid arrays with equal adjacent values like [1, 2, 2, 3].
Explore the code comparisonHide the code comparison
Using >= for non-decreasing
It rejects valid arrays with equal adjacent values like [1, 2, 2, 3].
Don't write this
if (nums[i-1] >= nums[i]) return false; // fails on duplicatesWrite this instead
if (nums[i-1] > nums[i]) return false;Revise
Carry the pattern into the interview
Pattern to remember
Adjacent-pair invariant
Code pattern to memorize
for i in 1..n-1: if a[i-1] > a[i] return false; return trueRules to remember
- 1Check neighbours only.
- 2Use
>for non-decreasing. - 30/1 elements are sorted.
Use this pattern when
- Verifying monotonicity
- Pre-checks before binary search
Interview takeaway
Global order follows from every adjacent pair being ordered.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Adjacent-pair invariant. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Check If Array Is Sorted and RotatedComing soonAt most one 'wrap' inversion is allowed.