Quick Overview

An easy array problem from a machine learning engineer online assessment: given positive integers and an inclusive value range, return the smallest element whose value falls inside the range, or 0 when none does. It tests careful reading of range semantics, inclusive bounds and a sentinel return value.

Minimum Array Value Within an Inclusive Value Range

Company: Capital One

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Online Assessment

You are given an array of positive integers and an inclusive value range `[low, high]`. Return the smallest array element whose value lies in the range. If no element lies in the range, return 0. ### Function Signature ```python def min_value_in_range(nums: list[int], low: int, high: int) -> int: ``` ### Rules - An element `x` lies in the range when `low <= x <= high`; both bounds are inclusive. - The range applies to element values, not to positions in the array. - Return 0 when no element qualifies. Every element is positive, so 0 can never be a real minimum. ### Constraints - `1 <= len(nums) <= 10^5` - `1 <= nums[i] <= 10^9` - `1 <= low <= high <= 10^9` ### Examples **Example 1** ```text Input: nums = [8, 3, 15, 6, 11], low = 5, high = 12 Output: 6 ``` The elements 8, 6 and 11 lie in `[5, 12]`, and the smallest of them is 6. **Example 2** ```text Input: nums = [1, 2, 20], low = 5, high = 10 Output: 0 ``` No element lies in `[5, 10]`. **Example 3** ```text Input: nums = [4, 9, 4], low = 4, high = 4 Output: 4 ``` Both bounds are inclusive, so each 4 qualifies.

Overview: An easy array problem from a machine learning engineer online assessment: given positive integers and an inclusive value range, return the smallest element whose value falls inside the range, or 0 when none does. It tests careful reading of range semantics, inclusive bounds and a sentinel return value.

Read the full Capital One Machine Learning Engineer interview experience this question came from

You are given an array of positive integers `nums` and an inclusive value range `[low, high]`. Return the smallest element of `nums` whose value lies in the range. If no element lies in the range, return `0`. ### Rules - An element `x` lies in the range when `low <= x <= high`; both bounds are inclusive. - The range applies to element values, not to positions in the array. - Return `0` when no element qualifies. Every element is positive, so `0` can never be a real minimum. ### Constraints - `1 <= len(nums) <= 10^5` - `1 <= nums[i] <= 10^9` - `1 <= low <= high <= 10^9` No value, including the returned one, can exceed 10^9, so every value fits in a signed 32-bit integer (it never exceeds 2^31 - 1); `int` is enough in Java and C++. ### Examples **Example 1** ```text Input: nums = [8, 3, 15, 6, 11], low = 5, high = 12 Output: 6 ``` The elements 8, 6 and 11 lie in `[5, 12]`, and the smallest of them is 6. **Example 2** ```text Input: nums = [1, 2, 20], low = 5, high = 10 Output: 0 ``` No element lies in `[5, 10]`.

Constraints

  • 1 <= len(nums) <= 10^5
  • 1 <= nums[i] <= 10^9
  • 1 <= low <= high <= 10^9

Examples

Input: ([8, 3, 15, 6, 11], 5, 12)

Expected Output: 6

Explanation: Source Example 1: 8, 6 and 11 lie in [5, 12]; the smallest of them is 6.

Input: ([1, 2, 20], 5, 10)

Expected Output: 0

Explanation: Source Example 2: elements sit on both sides of [5, 10] but none inside, so the answer is 0.

Hints

  1. Both ends of the range are inclusive: an element equal to low or to high counts.
  2. The range filters element values, not positions, and the smallest element of the whole array may fall outside it.
  3. Every element is at least 1, so 0 can safely stand for the case where nothing qualifies.

Smallest Array Value Within an Inclusive Range

hardCapital OneMachine Learning Engineer

You are given an array of positive integers nums and an inclusive value range [low, high]. Return the smallest element of nums whose value lies in the range. If no element lies in the range, return 0.

Rules

  • An element x lies in the range when low <= x <= high; both bounds are inclusive.
  • The range applies to element values, not to positions in the array.
  • Return 0 when no element qualifies. Every element is positive, so 0 can never be a real minimum.

Constraints

  • 1 <= len(nums) <= 10^5
  • 1 <= nums[i] <= 10^9
  • 1 <= low <= high <= 10^9

No value, including the returned one, can exceed 10^9, so every value fits in a signed 32-bit integer (it never exceeds 2^31 - 1); int is enough in Java and C++.

Examples

Example 1

Input:  nums = [8, 3, 15, 6, 11], low = 5, high = 12
Output: 6

The elements 8, 6 and 11 lie in [5, 12], and the smallest of them is 6.

Example 2

Input:  nums = [1, 2, 20], low = 5, high = 10
Output: 0

No element lies in [5, 10].

Examples

Example 1
Input
([8, 3, 15, 6, 11], 5, 12)
Output
6
Notes
Source Example 1: 8, 6 and 11 lie in [5, 12]; the smallest of them is 6.
Example 2
Input
([1, 2, 20], 5, 10)
Output
0
Notes
Source Example 2: elements sit on both sides of [5, 10] but none inside, so the answer is 0.

Constraints

  • •1 <= len(nums) <= 10^5
  • •1 <= nums[i] <= 10^9
  • •1 <= low <= high <= 10^9

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def min_value_in_range(nums: list[int], low: int, high: int) -> int:
Sample call
min_value_in_range([8, 3, 15, 6, 11], 5, 12)
Language
Case 1
Input
([8, 3, 15, 6, 11], 5, 12)
Expected
6
Case 2
Input
([1, 2, 20], 5, 10)
Expected
0

Browse More Questions

Show the approach

Approach

Scan the array once and keep best, the smallest qualifying value seen so far, where 0 means that nothing has qualified yet. For each element x, if low <= x <= high and either best == 0 or x < best, set best = x. Invariant: after the first i elements, best equals the minimum of the qualifying elements among them, or 0 if none of them qualifies. Every element is at least 1, so the sentinel 0 can never be confused with a real qualifying value, and after the last element best is exactly the required answer. Edge cases: elements equal to low or to high qualify because both bounds are inclusive; elements at low - 1 or high + 1 do not; the global minimum of the array is ignored when it lies below low; duplicates of the minimum do not change the result; the range is compared with values, never with indices; when no element qualifies the function returns 0 (an empty array, outside the stated constraints, also returns 0). All values are at most 10^9, so 32-bit integers suffice in every language.

Time complexity:
O(n)
Space complexity:
O(1)