Oracle Interview 8th March 2025

Anonymous User
1596
Mar 08, 2025
Mar 08, 2025

Whats wrong in this solution. Interviewer didn't accept it for higher values? I guess the lower bound and upper bound are actually calculated like this only.

def searchRange(self, nums: List[int], target: int) -> List[int]:
    n = len(nums)
    def lower_bound():
        low, high = 0,n-1
        ans = n
        while low<=high:
            mid = (low+high)//2
            if nums[mid]>=target:
                ans = mid
                high = mid-1
            else:
                low = mid +1
        return ans

    def upper_bound():
        low, high = 0,n-1
        ans = n
        while low<=high:
            mid = (low+high)//2
            if nums[mid]>target:
                ans = mid
                high = mid-1
            else:
                low = mid +1
        return ans
    if target not in nums: return [-1,-1]
    return [lower_bound(), upper_bound()-1]
Comments (4)