Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Second Largest Without Sorting
00:00
5 left

Second Largest Without Sorting

MediumPython

Problem

How would you find the second largest number in an unsorted array without using built-in sort functions?

Implement the function below to return the second distinct largest integer. The input contains at least two distinct values, and the result must be found without sorting or other built-in ordering functions.

Input: An array of integers.

Output: The second distinct largest integer.

Constraints

  • 2 <= nums.length <= 10^4
  • -10^9 <= nums[i] <= 10^9
  • The array contains at least two distinct integers
  • Do not use sorting or built-in ordering functions

Function Signature

def second_largest(nums):
Interviewer

Your question is Second Largest Without Sorting. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.