Quick Overview

Choose at least k nonadjacent array positions while minimizing the largest selected value. Preserve original adjacency and reason about threshold feasibility, greedy selection, and cases where eligible positions cluster.

Minimize the largest selected value across nonadjacent positions

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Online Assessment

Given an array of positive integers and an integer k, select at least k positions such that no two selected positions are adjacent. The cost of a selection is the largest selected value. Return the smallest achievable cost. ### Function Signature ```python def minimum_selection_cost(values: list[int], k: int) -> int: ``` ### Rules - Positions i and i + 1 cannot both be selected. - The original order cannot be changed. - Return only the minimum cost, not the selected positions. ### Constraints These bounds define this practice version. - `1 <= len(values) <= 100000`. - `1 <= values[i] <= 1000000000`, within signed 32-bit range. - `1 <= k <= (len(values) + 1) // 2`, so a selection exists. ### Examples ```text Input: values = [2, 3, 5, 9], k = 2 Output: 5 ``` Selecting indices 0 and 2 has cost 5. A smaller threshold cannot supply two nonadjacent positions. ```text Input: values = [4, 1, 7], k = 1 Output: 1 ``` Select only the middle position.

Overview: Choose at least k nonadjacent array positions while minimizing the largest selected value. Preserve original adjacency and reason about threshold feasibility, greedy selection, and cases where eligible positions cluster.

Read the full Microsoft Software Engineer interview experience this question came from

Given an array of positive integers and an integer k, select at least k positions such that no two selected positions are adjacent. The cost of a selection is the largest selected value. Return the smallest achievable cost. ### Function Signature ```python def minimum_selection_cost(values: list[int], k: int) -> int: ``` ### Rules - Positions i and i + 1 cannot both be selected. - The original order cannot be changed. - Return only the minimum cost, not the selected positions. ### Constraints These bounds define this practice version. - `1 <= len(values) <= 100000`. - `1 <= values[i] <= 1000000000`, within signed 32-bit range. - `1 <= k <= (len(values) + 1) // 2`, so a selection exists. ### Examples ```text Input: values = [2, 3, 5, 9], k = 2 Output: 5 ``` Selecting indices 0 and 2 has cost 5. A smaller threshold cannot supply two nonadjacent positions. ```text Input: values = [4, 1, 7], k = 1 Output: 1 ``` Select only the middle position.

Constraints

  • `1 <= len(values) <= 100000`.
  • `1 <= values[i] <= 1000000000`, within signed 32-bit range.
  • `1 <= k <= (len(values) + 1) // 2`, so a selection exists.

Examples

Input: ([2, 3, 5, 9], 2)

Expected Output: 5

Explanation: Two nonadjacent positions become feasible at threshold five.

Input: ([4, 1, 7], 1)

Expected Output: 1

Explanation: The minimum value is enough for one selection.

Hints

  1. For a fixed proposed cost, consider only positions whose values do not exceed it.
  2. If a cost is feasible, every larger proposed cost is feasible as well.

Minimize the largest selected value across nonadjacent positions

mediumMicrosoftSoftware Engineer

Given an array of positive integers and an integer k, select at least k positions such that no two selected positions are adjacent. The cost of a selection is the largest selected value. Return the smallest achievable cost.

Function Signature

def minimum_selection_cost(values: list[int], k: int) -> int:

Rules

  • Positions i and i + 1 cannot both be selected.
  • The original order cannot be changed.
  • Return only the minimum cost, not the selected positions.

Constraints

These bounds define this practice version.

  • 1 <= len(values) <= 100000.
  • 1 <= values[i] <= 1000000000, within signed 32-bit range.
  • 1 <= k <= (len(values) + 1) // 2, so a selection exists.

Examples

Input: values = [2, 3, 5, 9], k = 2
Output: 5

Selecting indices 0 and 2 has cost 5. A smaller threshold cannot supply two nonadjacent positions.

Input: values = [4, 1, 7], k = 1
Output: 1

Select only the middle position.

Examples

Example 1
Input
([2, 3, 5, 9], 2)
Output
5
Notes
Two nonadjacent positions become feasible at threshold five.
Example 2
Input
([4, 1, 7], 1)
Output
1
Notes
The minimum value is enough for one selection.

Constraints

  • •`1 <= len(values) <= 100000`.
  • •`1 <= values[i] <= 1000000000`, within signed 32-bit range.
  • •`1 <= k <= (len(values) + 1) // 2`, so a selection exists.

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 minimum_selection_cost(values: list[int], k: int) -> int:
Sample call
minimum_selection_cost([2, 3, 5, 9], 2)
Language
Case 1
Input
([2, 3, 5, 9], 2)
Expected
5
Case 2
Input
([4, 1, 7], 1)
Expected
1

Browse More Questions

Show the approach

Approach

Test a threshold by scanning left to right. Take the earliest remaining eligible position and skip its immediate neighbor. In an optimal feasible set, replacing its first selection with this earlier eligible position cannot create an adjacency with any following selection. Repeating the exchange proves the scan maximizes the number of eligible positions.

Binary search for the smallest threshold whose greedy count is at least k. Feasibility is monotone, since raising the threshold never removes an eligible position. The maximum array value is always feasible under the stated bound on k.

Time complexity:
O(n log(V + 1)) for value range width V
Space complexity:
O(1)