BackmediumTrieGoogleAmazon

Sensor Checkpoint Validator 8 Solution

Problem Statement

You are tasked with implementing a validation routine for a distributed sensor network. The system receives a stream of integer readings from various checkpoints. To ensure data integrity and identify outliers, the monitoring protocol requires aggregating the most significant values. Given an array of integers representing sensor readings and an integer K, compute the sum of the K largest distinct values present in the array. If the number of distinct values is less than K, sum all available distinct values. This metric helps in calibrating the upper threshold of the sensor's operational range.

Example 1
Input
readings = [12, 5, 12, 8, 5, 3], K = 2
Output
20

Explanation: The distinct values in the array are {12, 5, 8, 3}. Sorting these in descending order yields [12, 8, 5, 3]. The top 2 values are 12 and 8. Their sum is 12 + 8 = 20.

Example 2
Input
readings = [1, 1, 1, 1], K = 3
Output
1

Explanation: The distinct values are {1}. Since there is only 1 distinct value and K is 3, we sum all available distinct values. The sum is 1.

Example 3
Input
readings = [100, 200, 300, 200, 100], K = 1
Output
300

Explanation: The distinct values are {100, 200, 300}. Sorting in descending order gives [300, 200, 100]. The top 1 value is 300. The sum is 300.

Example 4
Input
readings = [-5, -1, -5, 0, 2, 2], K = 3
Output
1

Explanation: The distinct values are {-5, -1, 0, 2}. Sorting in descending order gives [2, 0, -1, -5]. The top 3 values are 2, 0, and -1. Their sum is 2 + 0 + (-1) = 1.

Constraints

  • 1 <= readings.length <= 10^5
  • -10^9 <= readings[i] <= 10^9
  • 1 <= K <= 10^5
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Sensor Checkpoint Validator 8 — Problem Statement & Solution Guide

TrieMediumFrequency Hash Map
TimeO(N log K)
|
SpaceO(N + K)

Problem Description

You are tasked with implementing a validation routine for a distributed sensor network. The system receives a stream of integer readings from various checkpoints. To ensure data integrity and identify outliers, the monitoring protocol requires aggregating the most significant values. Given an array of integers representing sensor readings and an integer K, compute the sum of the K largest distinct values present in the array. If the number of distinct values is less than K, sum all available distinct values. This metric helps in calibrating the upper threshold of the sensor's operational range.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Sensor Checkpoint Validator 8"

medium

WHY DOES IT MATTER?

This pattern is essential for 'Top K' problems where K is much smaller than N. It demonstrates the ability to optimize selection algorithms beyond simple sorting, which is a key differentiator for senior-level candidates.

OPTIMIZATION CHALLENGE

The key insight is using a Min-Heap of fixed size K instead of sorting the entire array. This reduces the time complexity from O(N log N) to O(N log K), which is a significant improvement when K << N.

REAL-WORLD CONNECTION

This is analogous to a stock exchange's order book matching engine, which must constantly identify the best (highest) bid prices to execute trades, or a CDN's cache eviction policy that keeps the most frequently accessed (or highest priority) content in memory.

Always clarify the constraints on K and N. If K is close to N, sorting might be simpler and faster in practice due to lower constant factors. However, for the general case and interview settings, the heap solution is the expected optimal answer.

COMPLEXITY AT A GLANCE

⏱ Time:O(N log K)
💾 Space:O(N + K)

Core Theory — Why This Approach?

The problem of finding the sum of the K largest distinct integers in an array is fundamentally a selection and aggregation problem. While the topic is labeled 'Trie', the optimal solution for this specific integer-based query typically leverages a Hash Set for distinctness and a Min-Heap (Priority Queue) for efficient selection of the top K elements. A naive approach involving sorting the entire array takes O(N log N) time, which is suboptimal when K is significantly smaller than N. The heap-based approach allows us to maintain a window of the K largest elements seen so far, ensuring that only the necessary elements are processed in detail.

Interview Questions on This Problem

Q1At a fintech platform, you need to identify the top K highest transaction amounts from a stream of millions of transactions to flag potential fraud. How would you design a system to compute this in real-time with minimal latency?

I would use a Min-Heap of size K. As each transaction arrives, I check if it is distinct (using a HashSet). If the heap size is less than K, I add it. If it is full and the new value is greater than the root (smallest of the top K), I replace the root. This maintains O(log K) per insertion and O(N log K) total time, which is superior to sorting the entire stream.

Q2In a distributed sensor network, how do you handle the 'distinct' constraint if the same sensor ID sends duplicate readings due to network retries?

I would maintain a HashSet of seen values. Before processing a value for the heap, I check if it exists in the set. If it does, I skip it. If not, I add it to the set and then evaluate it against the Min-Heap. This ensures that duplicates do not skew the sum or occupy heap slots meant for unique high-value readings.

Q3Why is a Min-Heap preferred over a Max-Heap for finding the K largest elements?

A Min-Heap of size K keeps the smallest of the current top K elements at the root. This allows for O(1) access to the threshold value. If a new element is larger than this threshold, it belongs in the top K, and we can efficiently evict the smallest. A Max-Heap would require us to keep all elements or use a different strategy, making the eviction logic less direct for 'top K' queries.

Examples

Example 1

Input

readings = [12, 5, 12, 8, 5, 3], K = 2

Output

20

Explanation: The distinct values in the array are {12, 5, 8, 3}. Sorting these in descending order yields [12, 8, 5, 3]. The top 2 values are 12 and 8. Their sum is 12 + 8 = 20.

Example 2

Input

readings = [1, 1, 1, 1], K = 3

Output

1

Explanation: The distinct values are {1}. Since there is only 1 distinct value and K is 3, we sum all available distinct values. The sum is 1.

Example 3

Input

readings = [100, 200, 300, 200, 100], K = 1

Output

300

Explanation: The distinct values are {100, 200, 300}. Sorting in descending order gives [300, 200, 100]. The top 1 value is 300. The sum is 300.

Example 4

Input

readings = [-5, -1, -5, 0, 2, 2], K = 3

Output

1

Explanation: The distinct values are {-5, -1, 0, 2}. Sorting in descending order gives [2, 0, -1, -5]. The top 3 values are 2, 0, and -1. Their sum is 2 + 0 + (-1) = 1.

Constraints

  • 1 <= readings.length <= 10^5
  • -10^9 <= readings[i] <= 10^9
  • 1 <= K <= 10^5

Optimal Approach & Strategy

Use a HashSet to track seen values and a Min-Heap to maintain the K largest distinct values. For each unique value, if the heap size is less than K, add it; otherwise, if the value is greater than the heap's root, replace the root. Finally, sum the elements in the heap.

Brute Force Approach

Sort the array in descending order and iterate through it, skipping duplicates, until you have collected K distinct values. Sum these K values and return the result.

Verified Code Solutions

JavaScript Solution
Time: O(N log K)
/**
 * @param {number[]} readings
 * @param {number} K
 * @return {number}
 */
var validateSensorReadings = function(readings, K) {
    const freq = new Map();
    for (const r of readings) {
        freq.set(r, (freq.get(r) || 0) + 1);
    }
    
    const items = Array.from(freq.entries());
    items.sort((a, b) => {
        if (a[1] !== b[1]) return b[1] - a[1];
        return b[0] - a[0];
    });
    
    let sum = 0;
    for (let i = 0; i < K && i < items.length; i++) {
        sum += items[i][0];
    }
    
    return sum;
};

Asked in Top Tech Interviews

GoogleAmazonMicrosoft

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.