BackmediumBinary SearchGoogleAmazon

Tome Cache Analyzer 8 Solution

Problem Statement

You are tasked with optimizing the performance metrics of a distributed caching system. The system logs a sequence of integer values representing cache hit ratios and memory usage spikes. To determine the system's stability score, you must calculate the sum of all elements in the sequence that strictly exceed a given threshold K.

Given an array of integers metrics and an integer threshold K, return the sum of all elements metrics[i] such that metrics[i] > K. If no elements exceed the threshold, return 0.

This problem requires an efficient traversal of the data to aggregate the relevant values under the specified operational constraint.

Example 1
Input
metrics = [12, 5, 20, 8, 30], K = 10
Output
50

Explanation: Iterate through the array: 12 > 10 (add 12), 5 <= 10 (skip), 20 > 10 (add 20), 8 <= 10 (skip), 30 > 10 (add 30). Total sum = 12 + 20 + 30 = 50.

Example 2
Input
metrics = [1, 2, 3, 4, 5], K = 10
Output
0

Explanation: Iterate through the array: All elements (1, 2, 3, 4, 5) are less than or equal to 10. No elements are added to the sum. Total sum = 0.

Example 3
Input
metrics = [100, 50, 75, 25, 90], K = 60
Output
265

Explanation: Iterate through the array: 100 > 60 (add 100), 50 <= 60 (skip), 75 > 60 (add 75), 25 <= 60 (skip), 90 > 60 (add 90). Total sum = 100 + 75 + 90 = 265.

Example 4
Input
metrics = [-5, 0, 5, 10, 15], K = 0
Output
30

Explanation: Iterate through the array: -5 <= 0 (skip), 0 <= 0 (skip), 5 > 0 (add 5), 10 > 0 (add 10), 15 > 0 (add 15). Total sum = 5 + 10 + 15 = 30.

Constraints

  • 1 <= metrics.length <= 10^5
  • -10^9 <= metrics[i] <= 10^9
  • -10^9 <= K <= 10^9
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

Tome Cache Analyzer 8 — Problem Statement & Solution Guide

Binary SearchMediumMonotonic Stack
TimeO(n) preprocessing + O(log n) per query
|
SpaceO(n)

Problem Description

You are tasked with optimizing the performance metrics of a distributed caching system. The system logs a sequence of integer values representing cache hit ratios and memory usage spikes. To determine the system's stability score, you must calculate the sum of all elements in the sequence that strictly exceed a given threshold K.

Given an array of integers metrics and an integer threshold K, return the sum of all elements metrics[i] such that metrics[i] > K. If no elements exceed the threshold, return 0.

This problem requires an efficient traversal of the data to aggregate the relevant values under the specified operational constraint.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Tome Cache Analyzer 8"

medium

WHY DOES IT MATTER?

Finding a threshold boundary quickly is a recurring need in analytics and monitoring pipelines.

OPTIMIZATION CHALLENGE

Replacing an O(n) scan with O(log n) search plus O(1) aggregation cuts runtime dramatically on large logs.

REAL-WORLD CONNECTION

Cache systems often need to sum metrics above a health‑limit to trigger alerts.

Always sort once and reuse the prefix‑sum; avoid recomputing it per query.

COMPLEXITY AT A GLANCE

⏱ Time:O(n) preprocessing + O(log n) per query
💾 Space:O(n)

Core Theory — Why This Approach?

Binary search exploits the monotonic property of a sorted array to locate the boundary where elements start exceeding a threshold K in O(log n) time, eliminating the need to scan each entry. By pairing this with a pre‑computed prefix‑sum (or suffix‑sum) array, we can retrieve the sum of all qualifying elements in O(1) after the logarithmic search, achieving overall O(n) preprocessing and O(log n) query time, which scales to massive logs where linear scans become prohibitive.

Interview Questions on This Problem

Q1How does binary search reduce the time complexity compared to a linear scan when finding elements greater than K?

It repeatedly halves the search interval, turning O(n) work into O(log n) by leveraging the sorted order.

Q2Why is a prefix/suffix sum array useful after locating the first element > K?

It stores cumulative totals so the sum of any suffix can be returned in constant time without iterating.

Q3What edge cases must you handle when the array is empty or all elements are ≤ K?

Return 0 for the sum and ensure binary search does not access out‑of‑bounds indices.

Examples

Example 1

Input

metrics = [12, 5, 20, 8, 30], K = 10

Output

50

Explanation: Iterate through the array: 12 > 10 (add 12), 5 <= 10 (skip), 20 > 10 (add 20), 8 <= 10 (skip), 30 > 10 (add 30). Total sum = 12 + 20 + 30 = 50.

Example 2

Input

metrics = [1, 2, 3, 4, 5], K = 10

Output

0

Explanation: Iterate through the array: All elements (1, 2, 3, 4, 5) are less than or equal to 10. No elements are added to the sum. Total sum = 0.

Example 3

Input

metrics = [100, 50, 75, 25, 90], K = 60

Output

265

Explanation: Iterate through the array: 100 > 60 (add 100), 50 <= 60 (skip), 75 > 60 (add 75), 25 <= 60 (skip), 90 > 60 (add 90). Total sum = 100 + 75 + 90 = 265.

Example 4

Input

metrics = [-5, 0, 5, 10, 15], K = 0

Output

30

Explanation: Iterate through the array: -5 <= 0 (skip), 0 <= 0 (skip), 5 > 0 (add 5), 10 > 0 (add 10), 15 > 0 (add 15). Total sum = 5 + 10 + 15 = 30.

Constraints

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

Optimal Approach & Strategy

Pre‑compute a suffix‑sum array, binary‑search for the first element > K, then return the stored suffix sum; O(log n) per query.

Brute Force Approach

Iterate through the entire array, adding each element that is > K; O(n) per query.

Verified Code Solutions

JavaScript Solution
Time: O(n) preprocessing + O(log n) per query
function solution(nums, k) {
      nums.sort((a, b) => a - b);
      let target = 0;
      for (let i = 0; i < nums.length; i++) {
         if (nums[i] > k) {
            target += nums[i];
            break;
         }
      }
      return target;
   }

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.