Median of Sorted List with Majority Element

Quick Overview

Find the median of a sorted integer list in which one value fills more than half of the positions, using constant time and constant space. It tests how you use the sorted order together with the majority guarantee, and whether you can justify your method for both odd and even lengths.

Median of Sorted List with Majority Element

Company: Okta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a sorted list of integers `nums` where more than 50% of the list is the same repeating integer, return the median of the list. The median is the middle value in an ordered list of numbers. If the list has an even number of elements, the median is the average of the two middle values. Your solution should run in `O(1)` time and use `O(1)` space. ### Function Signature ```python def find_median(nums: list[int]) -> float: ``` ### Rules - One value appears strictly more than `n / 2` times, where `n = len(nums)`. You may assume this guarantee always holds. - Indices are 0-based. - Return the median as a float, for example `2.0`. ### Constraints - `1 <= nums.length <= 10^5` - `nums` is sorted in non-decreasing order. - More than 50% of the elements in `nums` are the same integer. - `-10^9 <= nums[i] <= 10^9` ### Examples ```text Input: nums = [1, 2, 2, 2, 3] Output: 2.0 ``` The list has 5 elements and the value 2 appears 3 times (60%). The median is the middle element at index 2, which is 2. ```text Input: nums = [5, 5, 5, 5, 5, 5, 7, 8] Output: 5.0 ``` The list has 8 elements and the value 5 appears 6 times (75%). The two middle elements at indices 3 and 4 are both 5, so the median is `(5 + 5) / 2 = 5.0`. ```text Input: nums = [1, 9, 9, 9] Output: 9.0 ``` The value 9 appears 3 of 4 times. The middle elements at indices 1 and 2 are both 9, so the median is 9.0.

Overview: Find the median of a sorted integer list in which one value fills more than half of the positions, using constant time and constant space. It tests how you use the sorted order together with the majority guarantee, and whether you can justify your method for both odd and even lengths.

Okta logo
Okta
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a sorted list of integers nums where more than 50% of the list is the same repeating integer, return the median of the list.

The median is the middle value in an ordered list of numbers. If the list has an even number of elements, the median is the average of the two middle values.

Your solution should run in O(1) time and use O(1) space.

Function Signature

def find_median(nums: list[int]) -> float:

Rules

  • One value appears strictly more than n / 2 times, where n = len(nums) . You may assume this guarantee always holds.
  • Indices are 0-based.
  • Return the median as a float, for example 2.0 .

Constraints

  • 1 <= nums.length <= 10^5
  • nums is sorted in non-decreasing order.
  • More than 50% of the elements in nums are the same integer.
  • -10^9 <= nums[i] <= 10^9

Examples

Input: nums = [1, 2, 2, 2, 3]
Output: 2.0

The list has 5 elements and the value 2 appears 3 times (60%). The median is the middle element at index 2, which is 2.

Input: nums = [5, 5, 5, 5, 5, 5, 7, 8]
Output: 5.0

The list has 8 elements and the value 5 appears 6 times (75%). The two middle elements at indices 3 and 4 are both 5, so the median is (5 + 5) / 2 = 5.0.

Input: nums = [1, 9, 9, 9]
Output: 9.0

The value 9 appears 3 of 4 times. The middle elements at indices 1 and 2 are both 9, so the median is 9.0.