Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Kth Largest Element

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Kth Largest Element. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Find the Kth Largest Element in an Array (LeetCode 215).

Asked in the Virtual Onsite stage. Candidate provided an O(n log k) heap solution and the interviewer moved on.

Write a function that returns the kth largest element in an integer array, where k is 1-based and duplicates count as separate values.

Function

def findKthLargest(nums, k):

Input

  • nums: list of integers
  • k: integer

Output

  • Return the kth largest integer in nums.

Constraints

  • 1 <= k <= len(nums)
  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9

Function Signature

def findKthLargest(nums, k):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output