As reported: HSBC Trainee Software Engineer, campus hiring (2026), technical interview with live coding on JDoodle: one of three simple coding questions was finding the second largest element of an array, followed by questions on complexity and edge cases.
Given an array arr of positive integers, return the second largest element — the largest value that is strictly smaller than the maximum. If no such value exists (all elements are equal), return -1.
Example 1
arr = [12,35,1,10,34,1] -> 34
Example 2
arr = [10,5,10] -> 5
Example 3
arr = [10,10,10] -> -1
Constraints: 2 ≤ n ≤ 10^5; 1 ≤ arr[i] ≤ 10^5.
Code runner format: one JSON value per line: arr. Implement getSecondLargest(arr); the harness prints the value.