Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Failed test cases, correct solution

Suhaib AbdulQuddos

Feb 16, 2024

class Solution: def search(self,arr, key): if len(arr) == 0: return -1 if arr[0] == arr[-1]: return 0 if arr[0] == key else -1 inc = arr[-1] > arr[0] i = 0 j = len(arr)-1 while i < j: mid = (i+j)//2 if arr[mid] == key: return mid if arr[mid] > key and inc: j = mid - 1 else: i = mid +1 if arr[i] == key: return i return -1 # element not found

I have this solution, which should work. For example. for the array [1,1,1,1,1] it returns 0, but the solution says that it should return 2. I believe this is a mistake because the problem description does not specify how exactly duplicates should be handled.

1

0

Comments
Comments

On this page