FastPrepK Largest and K Smallest Values
Problems/K Largest and K Smallest Values

K Largest and K Smallest Values

Nutanix logoNutanix● EasyINTERNPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Nutanix intern candidate reported a second-round phone interview with some concept questions and two coding questions. According to the report, this one, the first coding question, asks for the k smallest and the k largest values of an integer array.

The report gives no order for the answers, no rule for repeated values, no limits and no examples. Here we chose the following. The function returns two lists: the k largest from largest down, and the k smallest from smallest up. Repeated values count separately. 1 <= k <= n <= 2000 with values from -10^5 to 10^5. The examples are ours.

Our version is about 85% the same as the reported question.

You are given an integer array nums and an integer k. Return two lists:

  1. the k largest values of nums, from largest to smallest;
  2. the k smallest values of nums, 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^5
  • 1 <= 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

Interview reports that mention this question

  • Nutanix SWE Intern Phone Screens: 20 Experiences (Part 3 of 3)

Nutanix interview experiences

  • Nutanix SWE Intern OA Questions: 16 Experiences (Part 2 of 3)
  • Nutanix U.S. SWE Intern Interviews: 54 Experiences (Part 1 of 3)
  • Nutanix SWE OA and Interviews, Level Not Reported: 9 Experiences
  • Nutanix Intern Debugging Round and Interviews: 25 Experiences
All Nutanix interview questionsSee Nutanix hiring insights
Saved
class Solution {
    public int[][] kLargestAndSmallest(int[] nums, int k) {
        // Write your code here.
    }
}
Checking account…