## Pregunta de entrevista

Entrevista de Data Engineer

-

# Given an array of integers, we would like to determine whether the array is monotonic (non-decreasing/non-increasing) or not. Examples: // 1 2 5 5 8 // true // 9 4 4 2 2 // true // 1 4 6 3 // false //1 1 1 1 1 1 // true

Respuesta

## Respuestas de entrevistas

19 respuestas

4

if l == sorted(l) or l == sorted(l,reverse=True): print(True) else: print(False)

Abhishek Goel en

3

python solution. inc = dec = True for i in range(len(nums)-1): if nums[i] > nums[i+1]: inc = false if nums[i] < nums[i+1]: dec = false return inc or dec

Anónimo en

1

An array is monotonic if and only if it is monotone increasing, or monotone decreasing. Since p = A[i+1] for all i indexing from 0 to len(A)-2. Note: Array with single element can be considered to be both monotonic increasing or decreasing, hence returns “True“. --------------------Python 3 code---------------------- # Check if given array is Monotonic def isMonotonic(A): return (all(A[i] = A[i + 1] for i in range(len(A) - 1))) # Test with an Array A = [6, 5, 4, 4] # Print required result print(isMonotonic(A))

Yusuf Arif en

0

def array_is_monotonic(array): return len(set(["gte" if x >= array[i+1] else "lt" for i, x in enumerate(array[:-1])])) == 1 print("[1, 2, 5, 5, 8] --> true:", array_is_monotonic([1, 2, 5, 5, 8])) print("[9, 4, 4, 2, 2] --> true:", array_is_monotonic([9, 4, 4, 2, 2])) print("[1, 4, 6, 3] --> false:", array_is_monotonic([1, 4, 6, 3])) print("[1, 1, 1, 1, 1, 1] --> true:", array_is_monotonic([1, 1, 1, 1, 1, 1]))

Carlos Alvidrez en

0

def ismonotonict(List) : return ( all (A[i] = A[i+1] for i in range(len(A)-1))

Simple way en

0

def ismonotonict(List) : return ( all (A[i] = A[i+1] for i in range(len(A)-1))

Simple way en

0

The solutions above is not accurate, just assigns A[i] to A[i+1] but doesn't incorporate increasing/decreasing values in array. A clearer way would be to do it as follows: def is_monotonic(A): n=len(A) return ( all(A[i] = a[i+1] for i in range(n-1)) )

Kuma en

0

Answer got cut off.. def is_monotonic(a): n=len(a) return (all(a[i] = a[i+1] for i in range(n-1))

Kuma en

0

Python Loop.. class Solution(object): def isMonotonic(self, A): """ :type A: List[int] :rtype: bool """ j = 0 k = 0 for i in range(len(A)-1): if A[i] A[i+1]: k += 1 else: j += 1 k += 1 if j == (len(A)-1) or k == (len(A)-1): return True else: return False

Anónimo en

0

The key in these questions is to cover the fundamentals, and be ready for the back-and-forth with the interviewer. Might be worth doing a mock interview with one of the Facebook or ex-Facebook Data Engineer experts on Prepfully? They give real-world practice and guidance, which is pretty helpful. prepfully.com/practice-interviews

Anónimo en

0

def monotonic(L): if L: return (all(x >= y for x,y in zip(L,L[1:])) or all(x <= y for x,y in zip(L,L[1:]))) else: return False L = [1, 2, 5, 5, 8] L = [9,4,4,2,2] L = [1,4,6,3] L = [1,1,1,1,1,1] monotonic(L)

Anónimo en

0

fun monotonic(array: IntArray) : Boolean{ if(array.isEmpty() || array.size array[i+1]){ if(increasing == false){ return false } increasing = true }else if(array[i] < array[i+1]){ if(increasing == true){ return true } increasing = false } } return true }

Rajan en

0

#first function: ever increasing? def is_increasing(lst): for i in range(1,len(lst)): if lst[i] lst[i-1]: return False return True # True OR True: then yes. def monontonic def is_monotonic(lst): if is_increasing(lst) == True or is_decreasing(lst) == True: return True else: return False

mockinterview.co en

0

def setUp(): integers = list(map(int,input().strip().split())) return integers def isMonotonic(int_list): monotonic = True if len(int_list) int_list[i-1]: return "non-monotonic" else: if int_list[i] int_list[i-1]: i_or_d = 'i' if i_or_d == "n": return "neighter" elif not monotonic: return "Non-monotonic" else: return "Monotonic" if __name__ == '__main__': int_list = setUp() print("Monotonic or Non-monotonic?: ", isMonotonic(int_list))

Chandan en

0

def setUp(): integers = list(map(int,input().strip().split())) return integers def isMonotonic(int_list): monotonic = True if len(int_list) int_list[i-1]: return "non-monotonic" else: if int_list[i] int_list[i-1]: i_or_d = 'i' if i_or_d == "n": return "neighter" elif not monotonic: return "Non-monotonic" else: return "Monotonic" if __name__ == '__main__': int_list = setUp() print("Monotonic or Non-monotonic?: ", isMonotonic(int_list))

Anónimo en

0

def isMonotonic(array): direction = 0 for i in range(1, len(array)): if array[i] > array[i-1]: if direction == -1: return False else: direction = 1 if array[i] < array[i-1]: if direction == 1: return False else: direction = -1 return True

rcKahuna en

0

some of the above solutions do not work completely. they fail on the last digit. ex. arr=[11111,12558,357] def check_int(num): a=str(num) for idx, digit in enumerate(a): if idx != len(str(num)) -1: check_idx= idx + 1 else: return False if digit == a[check_idx]: return True for digits in arr: print(check_int(digits))

hw en

0

return ( all (A[i] >= A[i+1] for i in range(len(A)-1)) or all(A[i] <= A[i+1] for i in range(len(A)-1 )) )

Anónimo en

1

Easy question.. The "loop way" (Exactly one pass over the array but bulky code): -> return True is the sequence has size less than 3 -> loop through from start until you find a sequential pair that is unequal and determine whether the array is increasing or decreasing, -> continue the loop now where you left off and check each pair of words whether they increase (if array is supposed to be increasing) or decrease (if array is supposed to be decreasing). -> return false when the condition is not met, and return true if you finish the loop without having the condition being violated Algebraic way (a few passes over the array but tidy code): -> again return true if array has less than 3 elements -> Create 2 copies of the array, one with the first element removed and the other the last element removed -> subtract the first copy from the second copy -> check in the resulting array whether all values are at least 0 (monotonically increasing) or all values are at most 0 (monotonically decreasing) -> If neither applies, return false -> if one applies, check whether the relation between arr and arr is also the same and if so return true, if not return false

Anónimo en