Pregunta de entrevista de ADP

1) Write a function that returns the second highest number within an array 2) Write a function that returns the most occurring letter in a string 3) Combine the above ^ into one function I would say in as much as leetcode helps, study data structures as much as possible because the questions may not even be there and you'd have to figure it out yourself

Respuesta de la entrevista

Anónimo

28 de nov de 2023

1) //second highest number class SecondHighestNumber { public static int findSecondHighest(int[] arr) { if (arr.length < 2) { System.out.println("The array should have at least two elements"); return -1; } int firstMax = Math.max(arr[0], arr[1]); int secondMax = Math.min(arr[0], arr[1]); for (int i = 2; i < arr.length; i++) { if (arr[i] > firstMax) { secondMax = firstMax; firstMax = arr[i]; } else if (arr[i] > secondMax && arr[i] < firstMax) { secondMax = arr[i]; } } return secondMax; }