Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Implement Nth Largest Number

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

Your question is Implement Nth Largest Number. 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

An Airtel Payments Bank analytics pipeline receives an unsorted array of integer transaction values. Implement a function that returns the nth largest value, counting duplicate values as separate elements.

You must avoid fully sorting the array. Use an appropriate data structure to maintain only the values needed to determine the result.

Formal Specification

Implement nth_largest(nums, n).

  • Input: nums, a non-empty list of integers, and n, a 1-indexed integer.
  • Output: The integer that would appear at position n when nums is sorted in descending order.
  • Duplicate values count independently. For example, the second largest value in [7, 7, 3] is 7.

Constraints

  • 1 <= len(nums) <= 10^5
  • 1 <= n <= len(nums)
  • -10^9 <= nums[i] <= 10^9
  • Duplicate values count as separate elements

Function Signature

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