Coding problem
Where a value starts and ends in a sorted list
The task
A shop's price list is sorted from low to high, and the same price can appear many times. To show "N products at this price", the shop needs the first and the last index of a price.
Complete FindRange(int[] values, int target). The array is sorted in ascending order. Return [first index, last index] of target, or [-1, -1] if it is not there. Each call should take O(log n) steps: the tests make 500,000 searches in an array of 1,000,000 values.
Signature
public static int[] FindRange(int[] values, int target)
Examples
[1, 2, 2, 2, 3], target 2 →[1, 3]. The 2s fill indexes 1 to 3.[1, 3, 5], target 4 →[-1, -1]. 4 would sit between 3 and 5, but it is not there.
Limits: Up to 1,000,000 values; values and the target can be int.MinValue or int.MaxValue.
The tests check: an empty array, one value (found and not found), every value equal to the target, a target below or above every value, int.MaxValue and int.MinValue as the target, 1,000,000 equal values, and 500,000 searches 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
{
// values is sorted ascending; returns [first, last] index of target, or [-1, -1].
public static int[] FindRange(int[] values, int target)
{
throw new NotImplementedException();
}
}This problem comes with TechInterviewNotes Full Access
Unlock all paid content on TechInterviewNotes.