Second Largest Element in an Array
Updated
Problem statement
Find the second largest value in an integer array nums, that is, the biggest number in it that is strictly less than the maximum, and return it.
A value counts once no matter how many times it appears, so no copy of the maximum can be the answer. nums always holds at least two different values, so an answer exists.
Function
secondLargest(nums: int[]) → intExamples
Example 1
nums = [41,-7,88,63,88,12]return = 63The maximum is 88, and it appears twice. Neither copy can be the answer. The other values are 41, -7, 63, 12, and the biggest of them is 63.
Example 2
nums = [-15,-40]return = -40The maximum is -15. The only value below it is -40, so that is the answer.
Example 3
nums = [5,9,9,9,5]return = 5The maximum 9 appears three times, and 5 appears twice. The only value below 9 is 5, so the answer is 5.
Constraints
2 <= nums.length <= 10^4-10^9 <= nums[i] <= 10^9numscontains at least two different values.
More Bank of America Onsite Interview questions
- Centered Star PyramidONSITE INTERVIEW · Seen Nov 2025
- Employees Without a ManagerONSITE INTERVIEW · Seen Nov 2025
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2025
- N-th Fibonacci NumberONSITE INTERVIEW · Seen Nov 2025
- Reverse Each Word in a SentenceONSITE INTERVIEW · Seen Nov 2025
- Second Highest SalaryONSITE INTERVIEW · Seen Nov 2025
- Three Numbers That Add Up to a TargetONSITE INTERVIEW · Seen Apr 2025
- Duplicate RowsONSITE INTERVIEW · Seen Oct 2023
- Factorial of a NumberONSITE INTERVIEW · Seen Oct 2023
- Palindrome String CheckONSITE INTERVIEW · Seen Sep 2023
Similar questions at other companies
- Group Transactions by HourAffirm · ONSITE INTERVIEWAffirm interview questions
- Merge Sorted ArrayDP World · ONSITE INTERVIEW
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Meeting RoomsGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Sort Products by Aisle With Frozen Items LastInstacart · ONSITE INTERVIEWInstacart interview questions
- Merge Two Arrays Into Sorted OrderNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Compress Consecutive Integer RangesDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Filter Anagrams of a TargetMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Rank Transform an ArrayAgoda · ONSITE INTERVIEWAgoda interview questions
- Sort Documents Left to RightFigma · ONSITE INTERVIEWFigma interview questions