Problem · Graph

Maximize Throughput (AMS)

Learn this problem
MediumTiktok logoTiktokNEW GRADOA
See Tiktok hiring insights

Problem statement

In ByteDance's vast network of data centers, millions of interconnected servers process content requests, handle user interactions, and deliver data to users globally. Each server is part of a dynamic task execution flow, represented by an array serverTasks, where each entry in the array indicates the next server in the chain that will handle a task.

Problem Description

Optimizing the data pipeline requires careful management of these task handoffs. Once a task is picked up by server i, it triggers a dependency on server serverTasks[i], transferring the load there. However, this data transfer disables both servers i and serverTasks[i] from participating in any further task handoffs, as they are locked due to processing the current load.

Therefore, selecting the right servers and managing the chain reactions of these task handoffs is crucial for maximizing throughput.

Each server at index i points to the next server serverTasks[i], where the task is transferred. Once this transfer occurs, both the sending server and the receiving server become unavailable for subsequent tasks. Your challenge is to select servers in such a way that maximizes the overall throughput score. The throughput score is determined by the sum of the indices of the servers where tasks are successfully handed off.

Task

Analyze this network of server-to-server task handoffs, navigate the dependencies, and determine the maximum possible throughput score that can be achieved by optimally choosing the task handoffs.

Input

Given the array serverTasks, calculate the maximum throughput score achievable by performing these operations in the most efficient way.

Function

calculateMaxProcessingThroughput(serverTasks: int[]) → long

Examples

Example 1

serverTasks = [0, 1, 2]return = 3

First, select the server at index 0. It points to itself, so its throughput is 0 and server 0 becomes unavailable.

Next, select the server at index 1. It also points to itself, so the total becomes 0 + 1 = 1 and server 1 becomes unavailable.

Finally, select the server at index 2. The total becomes 0 + 1 + 2 = 3.

Example 2

serverTasks = [2, 1, 0]return = 3

Example 3

serverTasks = [3, 0, 1, 2]return = 4

Select servers 1 and 3. The first handoff disables servers 1 and 0, and the second disables servers 3 and 2. All four servers are then unavailable, and the total throughput score is 1 + 3 = 4.

Constraints

  • 1 ≤ n ≤ 200000
  • 0 ≤ serverTasks[i] < n

More Tiktok problems

drafts saved locally
public long calculateMaxProcessingThroughput(int[] serverTasks) {
  // write your code here
}
serverTasks[0, 1, 2]
expected3
checking account