Wells Fargo Interview Question

Problem: Given an array of integers, find the maximum element in the array. Example: Input: [4, 9, 2, 7, 5] Output: 9

Interview Answer

Anonymous

Sep 26, 2023

def find_max(arr): if not arr: return None max_value = arr[0] for num in arr: if num > max_value: max_value = num return max_value # Example usage: arr = [4, 9, 2, 7, 5] result = find_max(arr) print(result) # Output will be 9 This code defines a function find_max that takes an array as input and returns the maximum element in the array. It initializes max_value with the first element and then iterates through the array, updating max_value whenever a larger element is encountered.