BackeasyLinked ListGoogleAmazon

Network Protocol Optimizer 47 Solution

Problem Statement

In a distributed network topology, a sequence of integer packets represents latency metrics collected from various nodes. To ensure stable protocol execution, the system must filter these metrics based on strict operational boundaries. You are provided with an array of integers and two boundary values, low and high. Your task is to compute the aggregate sum of all elements in the array that fall within the inclusive range [low, high].

This filtering process is critical for isolating valid data points from noise in the network stream. The solution must efficiently traverse the data structure to identify qualifying elements and accumulate their values. If no elements satisfy the condition, the result should be zero. The problem is framed within the context of recursive backtracking patterns, where the traversal logic can be conceptualized as a recursive descent through the list nodes, although an iterative approach is also valid for implementation.

Given the array metrics and the integers low and high, return the sum of all metrics[i] such that low <= metrics[i] <= high.

Example 1
Input
metrics = [12, 5, 8, 20, 3], low = 5, high = 10
Output
13

Explanation: We iterate through the array: 1. 12 is greater than 10, so it is ignored. 2. 5 is within [5, 10], so add 5 to the sum (sum = 5). 3. 8 is within [5, 10], so add 8 to the sum (sum = 13). 4. 20 is greater than 10, so it is ignored. 5. 3 is less than 5, so it is ignored. The final sum is 13.

Example 2
Input
metrics = [1, 2, 3, 4, 5], low = 10, high = 20
Output
0

Explanation: We iterate through the array: 1. 1 is less than 10, ignored. 2. 2 is less than 10, ignored. 3. 3 is less than 10, ignored. 4. 4 is less than 10, ignored. 5. 5 is less than 10, ignored. No elements fall within the range [10, 20]. The final sum is 0.

Example 3
Input
metrics = [100, 200, 300], low = 150, high = 250
Output
200

Explanation: We iterate through the array: 1. 100 is less than 150, ignored. 2. 200 is within [150, 250], so add 200 to the sum (sum = 200). 3. 300 is greater than 250, ignored. The final sum is 200.

Example 4
Input
metrics = [-5, 0, 5, 10], low = -10, high = 0
Output
-5

Explanation: We iterate through the array: 1. -5 is within [-10, 0], so add -5 to the sum (sum = -5). 2. 0 is within [-10, 0], so add 0 to the sum (sum = -5). 3. 5 is greater than 0, ignored. 4. 10 is greater than 0, ignored. The final sum is -5.

Constraints

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

Network Protocol Optimizer 47 — Problem Statement & Solution Guide

Linked ListEasyRecursive Backtracking
TimeO(n)
|
SpaceO(1)

Problem Description

In a distributed network topology, a sequence of integer packets represents latency metrics collected from various nodes. To ensure stable protocol execution, the system must filter these metrics based on strict operational boundaries. You are provided with an array of integers and two boundary values, low and high. Your task is to compute the aggregate sum of all elements in the array that fall within the inclusive range [low, high].

This filtering process is critical for isolating valid data points from noise in the network stream. The solution must efficiently traverse the data structure to identify qualifying elements and accumulate their values. If no elements satisfy the condition, the result should be zero. The problem is framed within the context of recursive backtracking patterns, where the traversal logic can be conceptualized as a recursive descent through the list nodes, although an iterative approach is also valid for implementation.

Given the array metrics and the integers low and high, return the sum of all metrics[i] such that low <= metrics[i] <= high.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Network Protocol Optimizer 47"

easy

WHY DOES IT MATTER?

Filtering by bounds is a fundamental pattern for sanitizing streams and enforcing invariants.

OPTIMIZATION CHALLENGE

The key is to avoid extra passes or auxiliary containers, reducing time to O(n) and space to O(1).

REAL-WORLD CONNECTION

Network routers drop packets outside acceptable latency windows, similar to discarding out‑of‑range nodes.

Maintain a moving pointer for the tail of the result list to splice nodes in‑place without extra allocations.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
💾 Space:O(1)

Core Theory — Why This Approach?

The problem reduces to counting (or extracting) nodes whose values lie within a closed interval [low, high]. A naïve scan of each element with a separate conditional check is O(n), but when the input is a linked list we cannot use random access; thus we must traverse sequentially, preserving O(1) auxiliary space. For massive streams or when the list is immutable, the optimal paradigm is a single-pass linear scan that updates a counter or builds a filtered list on‑the‑fly, avoiding extra passes or auxiliary data structures. This leverages the inherent sequential nature of linked lists and guarantees linear time while keeping memory overhead minimal.

Interview Questions on This Problem

Q1Why is a single-pass traversal preferred over converting the list to an array for this problem?

Converting incurs O(n) extra space and an additional O(n) copy cost. A single-pass respects the linked list's O(1) space constraint and runs in the same linear time.

Q2How would you modify the algorithm to return a new linked list containing only the valid nodes?

Create dummy head and tail pointers, appending nodes that satisfy low ≤ val ≤ high. This still runs in O(n) time and O(1) extra space besides the output list.

Q3What edge case must you handle when low > high?

The interval becomes empty, so the result should be zero (or an empty list). Detect this early to skip traversal if desired.

Examples

Example 1

Input

metrics = [12, 5, 8, 20, 3], low = 5, high = 10

Output

13

Explanation: We iterate through the array: 1. 12 is greater than 10, so it is ignored. 2. 5 is within [5, 10], so add 5 to the sum (sum = 5). 3. 8 is within [5, 10], so add 8 to the sum (sum = 13). 4. 20 is greater than 10, so it is ignored. 5. 3 is less than 5, so it is ignored. The final sum is 13.

Example 2

Input

metrics = [1, 2, 3, 4, 5], low = 10, high = 20

Output

0

Explanation: We iterate through the array: 1. 1 is less than 10, ignored. 2. 2 is less than 10, ignored. 3. 3 is less than 10, ignored. 4. 4 is less than 10, ignored. 5. 5 is less than 10, ignored. No elements fall within the range [10, 20]. The final sum is 0.

Example 3

Input

metrics = [100, 200, 300], low = 150, high = 250

Output

200

Explanation: We iterate through the array: 1. 100 is less than 150, ignored. 2. 200 is within [150, 250], so add 200 to the sum (sum = 200). 3. 300 is greater than 250, ignored. The final sum is 200.

Example 4

Input

metrics = [-5, 0, 5, 10], low = -10, high = 0

Output

-5

Explanation: We iterate through the array: 1. -5 is within [-10, 0], so add -5 to the sum (sum = -5). 2. 0 is within [-10, 0], so add 0 to the sum (sum = -5). 3. 5 is greater than 0, ignored. 4. 10 is greater than 0, ignored. The final sum is -5.

Constraints

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

Optimal Approach & Strategy

Perform a single linear pass, checking each node's value against low and high, updating a counter or linking it into a result list.

Brute Force Approach

Iterate through the list, and for each node perform a nested scan of the remaining nodes to verify range compliance, leading to O(n²) time.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(nums, constraints) {
      let sum = 0;
      for (let num of nums) {
         if (num >= constraints.min && num <= constraints.max) {
            sum += num;
         }
      }
      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.