Minimum and Maximum with Optimal Comparisons
Updated
Problem statement
Given a non-empty integer array values, find both its minimum and maximum elements.
Return a two-element array [minimum, maximum].
Your algorithm should pair elements so that it uses at most ceil(3 * n / 2) - 2 element comparisons, where n = values.length. This is fewer comparisons than finding the minimum and maximum independently.
Function
findMinMax(values: int[]) → int[]Examples
Example 1
values = [3,5,1,2,4,8]return = [1,8]The smallest value is 1 and the largest value is 8.
Example 2
values = [7]return = [7,7]With one element, the same value is both the minimum and the maximum.
Example 3
values = [-9,-2,-11,-3]return = [-11,-2]The minimum is -11 and the maximum is -2.
Constraints
1 ≤ values.length ≤ 100,000- Every element of
valuesis a signed 32-bit integer.
More InMobi Onsite Interview questions
- Russian Doll EnvelopesONSITE INTERVIEW · Seen Jul 2026
- Minimum Edges to Connect All ComponentsONSITE INTERVIEW · Seen Dec 2025
- Shortest Path Visiting All NodesONSITE INTERVIEW · Seen Sep 2025
- Maximum Sum of Non-Adjacent Tree NodesONSITE INTERVIEW · Seen Jan 2022
- Word SquaresONSITE INTERVIEW · Seen Jan 2022
- Maximum Length Pair ChainONSITE INTERVIEW · Seen Jul 2021
- Top View of a Binary TreeONSITE INTERVIEW · Seen Jul 2021
- Maximum XOR Between Two ArraysONSITE INTERVIEW · Seen May 2021
- Trapping Rain WaterONSITE INTERVIEW · Seen May 2021
- Validate Binary Search TreeONSITE INTERVIEW · Seen May 2021
Similar questions at other companies
- Count Distinct Pairs With Sum KeBay · ONSITE INTERVIEWeBay interview questions
- Best Time to Buy and Sell StockAmazon · ONSITE INTERVIEW
- Bad Request AlertsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Confusing Bib NumbersVerily · ONSITE INTERVIEW
- Group Transactions by HourAffirm · ONSITE INTERVIEWAffirm interview questions
- Longest Common PrefixKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Rotate a Matrix by 90 DegreesIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
- Plus OneDruva · ONSITE INTERVIEW
- Rotate a Flat Image ClockwiseGoogle · ONSITE INTERVIEWGoogle interview questions
- Merge Sorted ArrayDP World · ONSITE INTERVIEW