Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Median of ArrayList
00:00
5 left

Median of ArrayList

MediumPython

Problem

Write a logic to find the median value from an array list. Implement find_median(nums), where nums is a non-empty list of integers, and return the median as a number. For an odd-length list, return the middle value after sorting; for an even-length list, return the average of the two middle values. Example: [7, 1, 3] returns 3, while [1, 2, 3, 4] returns 2.5.

Constraints

  • 1 <= len(nums) <= 1000
  • -10^9 <= nums[i] <= 10^9
  • The input list is non-empty
  • Return the average of the two middle values for an even-length list

Function Signature

def find_median(nums):
Interviewer

Your question is Median of ArrayList. 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.