PracHub
QuestionsCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Amazon

Minimize Replacements So Equal Product Values Are Contiguous

Last updated: Jul 22, 2026

Quick Overview

Minimize the replacement cost needed so every remaining product value occupies one contiguous block, where one operation changes all occurrences of one value into another. The challenge tests compression of repeated runs, global merge effects, weighted decisions, and scalable reasoning for arrays with up to 200,000 entries.

  • hard
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Minimize Replacements So Equal Product Values Are Contiguous

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

# Minimize Replacements So Equal Product Values Are Contiguous You are given the quality value of each product in a line. In one operation, choose two distinct values `x` and `y` and replace every occurrence of `x` with `y`. The cost of the operation is the number of elements replaced. Find the minimum total cost needed to make every value that remains in the array occupy exactly one contiguous block. ```python def min_contiguous_replacement_cost(quality: list[int]) -> int: ... ``` You may perform any number of operations, and a replacement can merge a value into another value that already exists. Return only the minimum cost; you do not need to return the operations. ## Constraints - `0 <= len(quality) <= 200_000` - `-10^9 <= quality[i] <= 10^9` - The answer fits in a signed 64-bit integer. ## Examples ```text Input: quality = [1, 2, 1, 3, 3] Output: 1 Explanation: Replacing the single 2 with 1 makes each remaining value contiguous. ``` ```text Input: quality = [1, 2, 1, 2] Output: 2 ``` ```text Input: quality = [4, 4, 7, 7, 9] Output: 0 ```

Quick Answer: Minimize the replacement cost needed so every remaining product value occupies one contiguous block, where one operation changes all occurrences of one value into another. The challenge tests compression of repeated runs, global merge effects, weighted decisions, and scalable reasoning for arrays with up to 200,000 entries.

Related Interview Questions

  • Compute Edit Distance - Amazon (medium)
  • Find a Maximum-Sum Window in a Sparse Array - Amazon (hard)
  • Add One Source to Minimize Grid Inconvenience - Amazon (medium)
  • Implement a Half-Open Interval Range Module - Amazon (hard)
|Home/Coding & Algorithms/Amazon

Minimize Replacements So Equal Product Values Are Contiguous

Amazon logo
Amazon
Jul 12, 2026, 12:00 AM
hardSoftware EngineerOnsiteCoding & Algorithms
0
0

Minimize Replacements So Equal Product Values Are Contiguous

You are given the quality value of each product in a line. In one operation, choose two distinct values x and y and replace every occurrence of x with y. The cost of the operation is the number of elements replaced.

Find the minimum total cost needed to make every value that remains in the array occupy exactly one contiguous block.

def min_contiguous_replacement_cost(quality: list[int]) -> int:
    ...

You may perform any number of operations, and a replacement can merge a value into another value that already exists. Return only the minimum cost; you do not need to return the operations.

Constraints

  • 0 <= len(quality) <= 200_000
  • -10^9 <= quality[i] <= 10^9
  • The answer fits in a signed 64-bit integer.

Examples

Input: quality = [1, 2, 1, 3, 3]
Output: 1

Explanation: Replacing the single 2 with 1 makes each remaining value contiguous.
Input: quality = [1, 2, 1, 2]
Output: 2
Input: quality = [4, 4, 7, 7, 9]
Output: 0

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Amazon•More Software Engineer•Amazon Software Engineer•Amazon Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.