Median of an Array
Updated
Problem statement
You are given a nonempty integer array values. Its elements may be in any order and may contain duplicates. Return the median of all its values, counting every occurrence.
Let n be the array length. Consider the values in nondecreasing order, using zero-based indices:
- If
nis odd, the median is the value at indexfloor(n/2). - If
nis even, the median is the arithmetic mean of the values at indicesn/2 - 1andn/2.
Return one floating-point number. Under these bounds, the median is always an integer or a half-integer.
Do not change values. Each call must use its own working state and must not depend on earlier calls.
For a returned value answer, require |answer - exact| <= 0.000000001 * max(1, |exact|), where exact is the median.
Function
arrayMedian(values: int[]) → doubleExamples
Example 1
values = [3,1,4,1,5]return = 3.0In sorted order, the values are [1,1,3,4,5]. There are 5 values, so the median is the value at index 2, which is 3.
Example 2
values = [8,2,6,4]return = 5.0In sorted order, the values are [2,4,6,8]. The two central values are 4 and 6. Their arithmetic mean is (4 + 6)/2 = 5.
Example 3
values = [-5]return = -5.0The only value is -5, so the median is -5.
Constraints
1 <= values.length <= 60.- Every integer in
valuesis between-10^6and10^6, inclusive.
More Akuna Capital Phone Screen questions
- Match Orders by Price and Arrival TimeOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Biased Coin: Probability of Exactly h HeadsPHONE SCREEN · Seen Nov 2026
- Positive Nth Root with Gradient DescentPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Two SumOA · PHONE SCREEN · Seen Sep 2026
- Maximum Value in a Binary TreePHONE SCREEN · Seen Sep 2026
- Tiered Expiring Item StorePHONE SCREEN · Seen Aug 2026
- Statistics for the Latest K ValuesPHONE SCREEN · Seen Nov 2025
- Remove Duplicates While Preserving OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2025
- Sliding Window MaximumPHONE SCREEN · Seen Oct 2025
- Evaluate a PolynomialPHONE SCREEN · Seen Oct 2025
Similar questions at other companies
- Merge Overlapping IntervalsAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Merge Weekday and Time IntervalsNextdoor · PHONE SCREENNextdoor interview questions
- Peak Chat Load Across AgentsCresta · PHONE SCREENCresta interview questions
- 3SumAmazon · PHONE SCREEN
- Filter, Sort, and Deduplicate Scheduled TasksRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Longest Consecutive SequenceJP Morgan Chase · PHONE SCREENJP Morgan Chase interview questions
- Determine the Winner of Three-Card HandsOpenAI · PHONE SCREENOpenAI interview questions
- Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
- Top K Frequent Elements with a Recency TiebreakSwiggy · PHONE SCREEN
- Pins Violation Log QueriesPinterest · PHONE SCREENPinterest interview questions
Interview reports that mention this question
Akuna Capital interview experiences
- Akuna Capital Quant Trader and QR Interviews: 5 Experiences
- Akuna Capital JQR CodePair Rounds: 8 Experiences (Part 6 of 6)
- Akuna Capital QR Probability Phones: 14 Experiences (Part 5 of 6)
- Akuna Capital QR-ML Interviews: 11 Experiences (Part 4 of 6)
- Akuna Capital QR Coding Interviews: 17 Experiences (Part 2 of 6)