K Largest and K Smallest Values
Updated
Problem statement
You are given an integer array nums and an integer k. Return two lists:
- the
klargest values ofnums, from largest to smallest; - the
ksmallest values ofnums, from smallest to largest.
Return them as [largest, smallest]. Repeated values count separately: if the largest value appears twice and k is at least 2, it appears twice in the first list.
Function
kLargestAndSmallest(nums: int[], k: int) → int[][]Examples
Example 1
nums = [5,1,9,3,7]k = 2return = [[9,7],[1,3]]From largest to smallest the values are 9, 7, 5, 3, 1. The 2 largest are [9, 7]. The 2 smallest, from smallest up, are [1, 3].
Example 2
nums = [4,4,-2,4]k = 3return = [[4,4,4],[-2,4,4]]The value 4 appears three times and each copy counts. The 3 largest are [4, 4, 4]. The 3 smallest are -2 and then two of the 4s: [-2, 4, 4].
Example 3
nums = [6]k = 1return = [[6],[6]]There is one value and k = 1, so it is both the largest and the smallest: [[6], [6]].
Constraints
1 <= nums.length <= 2000-10^5 <= nums[i] <= 10^51 <= k <= nums.length
More Nutanix Phone Screen questions
- Swap Adjacent Linked List NodesPHONE SCREEN · Seen Mar 2025
- Top K Most Frequent ValuesPHONE SCREEN · Seen Mar 2025
- Unique Triplets Summing to ZeroPHONE SCREEN · Seen Sep 2024
- Find the Duplicate in a Read-Only ArrayPHONE SCREEN · Seen Apr 2021
- Kth Largest Element in an ArrayPHONE SCREEN · Seen Feb 2020
- Kth Smallest Element in a BSTPHONE SCREEN · Seen Feb 2020
- Outline of a Binary TreePHONE SCREEN · Seen Jan 2020
- Print All File and Folder PathsPHONE SCREEN · Seen Dec 2019
- Inorder Predecessor in a BSTPHONE SCREEN · Seen Jan 2019
- Odd Numbers Up and BackPHONE SCREEN · Seen Jan 2019
Similar questions at other companies
- Majority ElementByteDance · PHONE SCREENByteDance interview questions
- Recommend Films from Viewers Who Watched Everything You DidBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Total Years of Work ExperienceLinkedIn · PHONE SCREENLinkedIn interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Employee Count Median, Mean and ModeClay · PHONE SCREEN
- Squares of a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
- Sort by Checking PermutationsMercor · PHONE SCREENMercor interview questions
- Meeting RoomsGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Parse and Sort Integer DataLegora · PHONE SCREEN
- Rank Unseen Items by Embedding SimilarityYahoo · PHONE SCREENYahoo interview questions