Coding problem
Find a value in a rotated sorted list
The task
A ring buffer keeps order numbers in ascending order, but a reader may start anywhere in the ring, so it gets the numbers sorted and then turned: for example [5, 6, 7, 1, 2, 3].
Complete SearchRotated(int[] values, int target). values holds distinct ints that were sorted in ascending order and then rotated at an unknown point (possibly not at all). Return the index of target, or -1 if it is not there. Aim for O(log n) time per search. The array is never null.
Signature
public static int SearchRotated(int[] values, int target)
Examples
[5, 6, 7, 1, 2, 3], 1→3. 1 is the first value after the turn.[5, 6, 7, 1, 2, 3], 4→-1. 4 is not in the list.
Limits: Up to 1,000,000 values; the tests make up to 1,000,000 searches.
The tests check: an empty list, one value, a list that was not rotated, values before and after the turn, two-value lists, the first and last positions, and 1,000,000 searches in a list of 1,000,000 values in time.
Hints
Your code
Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;
public static class Solution
{
// Returns the index of target in a rotated sorted array of distinct values, or -1.
public static int SearchRotated(int[] values, int target)
{
throw new NotImplementedException();
}
}This problem comes with TechInterviewNotes Full Access
Unlock all paid content on TechInterviewNotes.