Second Largest Element
Updated
Problem statement
You are given an array arr of positive integers. Return the second largest value in it: the largest value that is strictly smaller than the largest value in the array.
Copies of the largest value do not count. For example, in [10, 5, 10] the answer is 5, not 10.
If no value is smaller than the largest one (for example, when every value is the same), return -1.
Function
secondLargest(arr: int[]) → intExamples
Example 1
arr = [12,35,1,10,34,1]return = 34The largest value is 35. The largest value below 35 is 34, so the answer is 34.
Example 2
arr = [10,5,10]return = 5The largest value is 10, and it appears twice. A second copy of 10 does not count, because the answer must be smaller than the largest value. The answer is 5.
Example 3
arr = [7,7,7]return = -1Every value is 7, so no value is smaller than the largest one. The answer is -1.
Constraints
1 <= arr.length <= 10^51 <= arr[i] <= 10^9
More Expedia Onsite Interview questions
- Scramble StringONSITE INTERVIEW · Seen Sep 2026
- Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
- Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
- Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
- Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
- Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
- Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
- Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024
Similar questions at other companies
- Group Transactions by HourAffirm · ONSITE INTERVIEWAffirm interview questions
- Longest Common PrefixKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Rotate a Matrix by 90 DegreesIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
- Plus OneDruva · ONSITE INTERVIEW
- Ring Buffer OperationsAmazon · ONSITE INTERVIEW
- Rotate a Flat Image ClockwiseGoogle · ONSITE INTERVIEWGoogle interview questions
- Merge Sorted ArrayDP World · ONSITE INTERVIEW
- List the Key and Value of Every Map EntryNewgen Software · ONSITE INTERVIEW
- Pizza Price CalculatorMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
Interview reports that mention this question
Expedia interview experiences
- Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
- Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
- Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
- Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
- Expedia SWE Intern Final Round: 126 Experiences (Part 1 of 6)