[TikTok] OA 2025 Start – 02 Feb (Generic)

6. Equalize Server Latency

TikTok’s server network is structured as a perfect binary tree with n servers, where servers are numbered from 0 to n-1. Each server is connected to its parent with the following configuration:

  • The parent of server i is floor((i-1)/2), for i ≥ 1.
    Here, floor(x) denotes the greatest integer less than or equal to x.
  • The children of server i are the servers numbered 2*i + 1 and 2*i + 2, if they exist.
  • The root server (server 0) has no parent.

The root server (server 0) handles requests, which are passed down to its child servers.

  • The latency cost between server i and its parent is defined as the time taken for data to travel along the edge between them and is given by latency[i-1] for i ≥ 1.
  • The latency from the root server to any leaf server is the sum of the latencies along the path from the root to that leaf.

The goal is to ensure that the latency from the root to every leaf server is the same. Currently, latencies along different paths may vary. You are allowed to increase the latency of some connections but cannot decrease any latency. You have to find the minimum amount of additional latency needed to equalize the latency from the root to all leaf servers.

Example

Suppose n = 7 and latency = [3, 1, 2, 1, 5, 4].

The given arrangement should look like this:


With latencies:

We can increment the latencies as follows:

  • Incrementing edge latency[0] by 1. (Connecting nodes 0 and 1)
  • Incrementing edge latency[3] by 1. (Connecting nodes 1 and 4)
  • Incrementing edge latency[5] by 1. (Connecting nodes 2 and 6)

After making the above increments, the cost from the root node to each leaf node is equal to 6. Since we have made a total of 3 increments, the answer is 3. It can be shown that the answer cannot be less than 3.

Function Description

Complete the function minAdditionalLatency.

minAdditionalLatency has the following parameters:

Sample Case 0

Sample Input For Custom Testing

STDIN          FUNCTION  
------        --------  
3          →  n = 3  
2          →  m = n - 1 = 2  
10 5       →  latency = [10, 5]

Sample Output

5

Explanation

Here, n = 3, and latency = [10, 5].

The given arrangement should look as follows:


Considering 0-based indexing, we can increment the latency for the connection between servers 0 and 2 by 5.

After making the increment, the distance from the root server to each leaf server will be equal to 10. Since we have made a total of 5 increments, the answer is 5.

It can be shown that the answer cannot be less than 5.


7. Maximum Positive Feedback

The TikTok Content creator’s videos receives feedback from viewers over time, represented as a binary string, videoFeedback:

  • 1 indicates positive reactions (likes, shares, or comments).
  • 0 indicates negative reactions (skips or dislikes).

The creator wants to improve a video's overall positive reception by strategically targeting a portion of the video for re-editing or improvement. Here's the strategy:

  • Choose one specific segment of the video(interval [i, j]) to leave unchanged, where i > 0 and j < videoFeedback_size.
  • Outside this segment, the plan is to completely change the content—flipping negative reactions to positive (0 to 1) and positive reactions to negative (1 to 0).

The objective is to maximize the total positive feedback (1s) for the entire video after performing this operation.

Note: The operation must be performed only once.


Example

Given videoFeedback_size = 6 and videoFeedback = "100110",

The interval i = 4 to j = 5 can be selected as the unchanged segment of the video.

Flipping the bits from index 1 to 3 transforms the string from "100110" to "011110".

Next, flipping the bit at index 6 (i.e., the last bit) changes the string from "011110" to "011111".

As this string contains 5 ones, the output should be 5.

There are no intervals that produce an output greater than 5.


Function Description

Complete the function getMaxPositiveFeedback.

def getMaxPositiveFeedback(videoFeedback: str) -> int:

Parameters:

  • videoFeedback: a binary string representing the feedback on a video, where 1 represents positive feedback and 0 represents negative reaction.

Returns:

  • int: the maximum positive feedback after an operation.

Constraints

  • 2 ≤ videoFeedback_size ≤ 3 × 10⁵
  • videoFeedback[i] = '0' or '1'

Sample Cases

Sample Case 0

Input
videoFeedback = "10000011"
Output
6
Explanation

Given videoFeedback_size = 8 and videoFeedback = "10000011",

We can select i = 7 and j = 7.

  1. Flip the bits from index 1 to 6. The feedback becomes "01111111".
  2. Next, flip the bit at index 8. The feedback becomes "01111110".

Hence, the answer is 6. It can be proven that the answer cannot be greater than 6.


Sample Case 1

Input
videoFeedback = "0100101"
Output
5
Explanation

Given videoFeedback_size = 7 and videoFeedback = "0100101",

We can select i = 2 and j = 2.

  1. Flip the bit at index 1. The feedback becomes "1100101".
  2. Flip the bits from index 3 to 7. The feedback becomes "1111010".

Hence, the answer is 5. It can be proven that the answer cannot be greater than 5.


我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。

We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.

Leave a Reply

Your email address will not be published. Required fields are marked *