DSA interview question
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.
bool isSorted(const vector<int>& a); // non-decreasing?
bool isSortedAndRotated(const vector<int>& a); // non-decreasing after some rotation?
Input a | isSorted | isSortedAndRotated | Explanation |
|---|---|---|---|
[10, 20, 30, 40, 50] | true | true | Rotation by 0 |
[90, 80, 100, 70, 40, 30] | false | false | Several drops |
[1, 1, 2, 2] | true | true | Equal neighbours are allowed |
[3, 4, 5, 1, 2] | false | true | One drop (5 → 1), and the last element ≤ the first |
[2, 1, 3, 4] | false | false | One drop, but 4 > 2 across the wrap |
[5] | true | true | |
[] | true | true | Vacuously sorted |
(Confirm: "sorted" means non-decreasing, so equal neighbours are fine.)
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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in