Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Second Largest Number in Array
00:00
5 left

Second Largest Number in Array

MediumPython

Problem

Write programming code to find the second largest number in an array.

Return the second largest distinct value, or None if fewer than two distinct values exist. Implement the function for an integer array, preserving O(n) time and O(1) extra space. Examples: [3, 1, 4, 2] returns 3; [5, 5] returns None. Constraints: 2 <= len(nums) <= 10^4 and values are integers.

Constraints

  • 2 <= len(nums) <= 10^4
  • -10^9 <= nums[i] <= 10^9
  • Return the second largest distinct value.
  • Return None when fewer than two distinct values exist.

Function Signature

def second_largest(nums):
Interviewer

Your question is Second Largest Number in Array. 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.