Domanda di colloquio di Meta

Giving a circular sorted array Ex: [7,9,3,5,6], write an algorithm that finds the smaller element index.

Risposte di colloquio

Anonimo

9 dic 2018

int findMinElement(int arr[], int size) { int low = 0; int high = size - 1; while (low <= high) { int middle = (low + high)/2; if ((arr[middle] < arr[middle - 1]) && (arr[middle] < arr[middle + 1])) { return arr[middle]; } else if (arr[middle] < arr[size - 1]) { high = middle - 1; } else { low = middle + 1; } } return -1; }

1

Anonimo

11 feb 2022

Usually this is supposed to be done in O(logn) time so linear approach will not be acceptable

Anonimo

3 gen 2019

func isOn(_ array: [Int],_ num: Int) -> Bool { var dictionary = [Int:Bool]() for curr in array { let currResult = num - curr if dictionary[currResult] == nil { dictionary[currResult] = true } else { return true } } return false } let arr = [1,1,4,12] let num = 2 print("isOn=\(isOn(arr, num))")

1