PPractick

DSA interview question

Check if an Array is Sorted

Easy · Algorithms · Topics: array

The problem

Before running a binary search, a data pipeline must confirm that each incoming batch of IDs is sorted in non-decreasing order, because binary search silently returns wrong answers on unsorted data. The check itself is one pass: an array is sorted exactly when every element is at least as large as the one before it. A popular variant (LeetCode 1752) asks whether the array is sorted after some rotation, like [3, 4, 5, 1, 2], and it only takes one extra observation.

Example: [10, 20, 30, 40, 50] → true; [90, 80, 100, 70] → false.

Function signature

bool isSorted(const vector<int>& a);            // non-decreasing?
bool isSortedAndRotated(const vector<int>& a);  // non-decreasing after some rotation?

Test cases

Input aisSortedisSortedAndRotatedExplanation
[10, 20, 30, 40, 50]truetrueRotation by 0
[90, 80, 100, 70, 40, 30]falsefalseSeveral drops
[1, 1, 2, 2]truetrueEqual neighbours are allowed
[3, 4, 5, 1, 2]falsetrueOne drop (5 → 1), and the last element ≤ the first
[2, 1, 3, 4]falsefalseOne drop, but 4 > 2 across the wrap
[5]truetrue
[]truetrueVacuously sorted

(Confirm: "sorted" means non-decreasing, so equal neighbours are fine.)

How interviewers phrase it

Clarifying questions to ask

  1. Non-decreasing (allowing equal neighbours) or strictly increasing?
  2. Ascending only, or either direction?
  3. For the rotated version: can the array contain duplicates?

Follow-ups to expect

  1. "What's the condition?"
  2. "How do you check sorted-and-rotated?"
  3. "Why include the wrap-around pair?"
  4. "Can you do it recursively?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in