BackmediumLinked ListGoogleAmazon

Pipeline Vector Extractor 25 Solution

Problem Statement

You are provided with the head of a singly linked list containing n integer nodes. The objective is to compute the aggregate sum of the three largest distinct values present in the sequence. If the list contains fewer than three nodes, or if there are fewer than three distinct values, the sum must include all available distinct values. The algorithm must operate in O(n) time complexity and utilize O(1) auxiliary space, excluding the input structure itself. You must traverse the list exactly once to identify the top three maximums without resorting to sorting or additional data structures like heaps or sets that scale with n.

Example 1
Input
head = [12, 5, 12, 8, 3, 12]
Output
25

Explanation: The distinct values in the list are {12, 5, 8, 3}. The three largest distinct values are 12, 8, and 5. Their sum is 12 + 8 + 5 = 25.

Example 2
Input
head = [1, 1, 1]
Output
1

Explanation: The only distinct value is 1. Since there are fewer than three distinct values, we sum all available distinct values. The sum is 1.

Example 3
Input
head = [100, 200, 300, 400, 500]
Output
1200

Explanation: The distinct values are {100, 200, 300, 400, 500}. The three largest are 500, 400, and 300. Their sum is 500 + 400 + 300 = 1200.

Example 4
Input
head = [-5, -1, -10, -2]
Output
-8

Explanation: The distinct values are {-5, -1, -10, -2}. The three largest (closest to positive infinity) are -1, -2, and -5. Their sum is -1 + (-2) + (-5) = -8.

Constraints

  • 1 <= n <= 10^5
  • -10^9 <= node.val <= 10^9
  • The linked list is guaranteed to be non-circular.
  • All values in the list are integers.
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

Pipeline Vector Extractor 25 — Problem Statement & Solution Guide

Linked ListMediumBFS / Union Find
TimeO(n)
|
SpaceO(1)

Problem Description

You are provided with the head of a singly linked list containing n integer nodes. The objective is to compute the aggregate sum of the three largest distinct values present in the sequence. If the list contains fewer than three nodes, or if there are fewer than three distinct values, the sum must include all available distinct values. The algorithm must operate in O(n) time complexity and utilize O(1) auxiliary space, excluding the input structure itself. You must traverse the list exactly once to identify the top three maximums without resorting to sorting or additional data structures like heaps or sets that scale with n.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Pipeline Vector Extractor 25"

medium

WHY DOES IT MATTER?

Finding top‑k distinct elements in a stream is a fundamental selection pattern used in many real‑time analytics.

OPTIMIZATION CHALLENGE

The key is reducing the O(n log n) sorting cost to an O(n) single‑pass update of constant‑size state.

REAL-WORLD CONNECTION

It mirrors maintaining a live leaderboard where only the highest scores are kept without storing every entry.

Implement three max variables and a small hash set; update them in descending order to avoid overwriting larger values.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
đź’ľ Space:O(1)

Core Theory — Why This Approach?

The problem is a variant of the classic selection algorithm: finding the top‑k distinct elements in a data stream. A naive solution would collect all node values, insert them into a set to remove duplicates, sort the set, and then sum the three largest, which costs O(n log n) time and O(n) extra space. The optimal paradigm leverages a single linear scan while maintaining only the three largest distinct values seen so far, using constant additional memory. By updating these three trackers whenever a new distinct value exceeds one of them, we achieve O(n) time and O(1) space, which scales gracefully even for very large linked lists.

Interview Questions on This Problem

Q1How do you ensure that duplicate values are not counted multiple times when tracking the top three?

Maintain a hash set of values already considered for the top three. Before updating any of the three max variables, check if the value is already in the set.

Q2What is the time and space complexity of the optimal solution versus a sorting‑based approach?

The optimal solution runs in O(n) time and O(1) extra space, while sorting after deduplication costs O(n log n) time and O(n) space. The linear scan eliminates the need for full sorting or large auxiliary structures.

Q3How would you generalize this algorithm to find the sum of the k largest distinct values?

Replace the three scalar trackers with a min‑heap of size k that stores the current top distinct values. Each new distinct element is compared to the heap root and inserted if larger, keeping the heap size fixed.

Examples

Example 1

Input

head = [12, 5, 12, 8, 3, 12]

Output

25

Explanation: The distinct values in the list are {12, 5, 8, 3}. The three largest distinct values are 12, 8, and 5. Their sum is 12 + 8 + 5 = 25.

Example 2

Input

head = [1, 1, 1]

Output

1

Explanation: The only distinct value is 1. Since there are fewer than three distinct values, we sum all available distinct values. The sum is 1.

Example 3

Input

head = [100, 200, 300, 400, 500]

Output

1200

Explanation: The distinct values are {100, 200, 300, 400, 500}. The three largest are 500, 400, and 300. Their sum is 500 + 400 + 300 = 1200.

Example 4

Input

head = [-5, -1, -10, -2]

Output

-8

Explanation: The distinct values are {-5, -1, -10, -2}. The three largest (closest to positive infinity) are -1, -2, and -5. Their sum is -1 + (-2) + (-5) = -8.

Constraints

  • 1 <= n <= 10^5
  • -10^9 <= node.val <= 10^9
  • The linked list is guaranteed to be non-circular.
  • All values in the list are integers.

Optimal Approach & Strategy

Traverse the list once, using a hash set to filter duplicates and three variables to track the top three distinct values, updating them in O(1) time per node.

Brute Force Approach

Collect all node values, deduplicate with a set, sort the set descending, and sum the first three elements.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(nums) {
   nums.sort((a, b) => b - a);
   return nums.slice(0, 3).reduce((a, b) => a + b, 0);
}

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.