VMWare | SWE | May 2022

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2022-05-05Added By Anonymous
Source: Link

give an unsorted list and return the index of element if element existed in list, if element does not existed return the index of closest value

1ex 2input: [], value: 3 => return -1 becuase no element inside list 3input: [1, 2, 3, 4, 5], value: 3 => return 2 # input[2] == 3 4input: [11, 12, 13,1 5,1 5], value: 14 => return 2 # 1] does not exist, input[2] == 13 , input[3] == 15, index 2 < index 3 5input: [11, 15, 13, 15, 11, 12, 13], value: 14 => return 1 # 14 does not exist, input[1] == 15, input[2] == 13, index 1 < index 2


note: this question can be solve by sort the input list first and binary search to return the index.


however if there is any way to solve with O(n) without sorting the list?

Comments