DSA interview question
The circular log buffer from the previous problem now allows repeated IDs (retries reuse the same ID), so an array might look like [2, 5, 6, 0, 0, 1, 2]. The question is just whether a given ID is present. The "one half is always sorted" trick still works, except in one ambiguous situation: when nums[lo], nums[mid] and nums[hi] are all equal, you can't tell which side holds the rotation. The fix is to discard both ends and continue, which costs O(n) in the worst case but stays O(log n) whenever duplicates don't get in the way.
Example: [2, 5, 6, 0, 0, 1, 2], target 0 → true.
bool searchRotatedWithDuplicates(const vector<int>& nums, int target);
Input (nums, target) | Output | Explanation |
|---|---|---|
([2, 5, 6, 0, 0, 1, 2], 0) | true | |
([2, 5, 6, 0, 0, 1, 2], 3) | false | |
([1, 0, 1, 1, 1], 0) | true | lo, mid and hi are all 1 at first |
([1, 1, 1, 1, 1, 1, 1, 1, 1, 13, 1, 1, 1, 1], 13) | true | Needs the shrink-both-ends step |
([1], 0) | false | |
([3, 1], 1) | true |
(Confirm: the array is a rotation of a non-decreasing array; duplicates are allowed; return whether the target exists.)
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