Fourth Largest Element in an Array
Updated
Problem statement
Given an integer array nums with at least four elements, return the fourth largest element.
The fourth largest element is the value in fourth place when nums is sorted from largest to smallest. Equal values count as separate elements, so the answer can equal a larger-ranked value.
Function
fourthLargest(nums: int[]) → intExamples
Example 1
nums = [7,2,9,4,1,8]return = 4From largest to smallest the values are 9, 8, 7, 4, 2, 1. The fourth one is 4.
Example 2
nums = [3,10,10,6,10]return = 6From largest to smallest the values are 10, 10, 10, 6, 3. The three copies of 10 take the first three places, so the fourth is 6.
Example 3
nums = [-5,-1,-9,-3]return = -9From largest to smallest the values are -1, -3, -5, -9. With exactly four elements, the fourth largest is the smallest one, -9.
Constraints
4 <= nums.length <= 10^4-10^9 <= nums[i] <= 10^9
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
- Merge Three Sorted ArraysMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEWHarvey interview questions
- Meeting Rooms III: Most Booked RoomGoogle · ONSITE INTERVIEWGoogle interview questions
- Meeting Rooms IISalesforce · ONSITE INTERVIEWSalesforce interview questions
- Top K Largest Values from a StreamThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
- Generate Complete Inputs for a Supplied ClassifierGeneral Motors · ONSITE INTERVIEW
- Minimum Meeting Rooms Without a Sweep LineGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Top K With a Deterministically Noisy Comparator (MLE)Mercor · ONSITE INTERVIEWMercor interview questions
- Top K Frequent ElementsCapgemini · ONSITE INTERVIEWCapgemini interview questions