SALESFORCE Coding Question – Solved

8 Live
2. Salesforce Latency Optimization Given a Salesforce infrastructure with API nodes, cloud regions, and API edges (bidirectional API connections between them), the i-th connection links regions api_from[i] and api_to[i] with a latency of api_weight[i]. The max-latency of a region is the maximum latency of any API within that region. Split this infrastructure into at most k independent regions by disabling some API connections such that the maximum of the max-latency of all regions formed is minimized. Given api_nodes, api_from, and api_to, determine the minimum possible value of the maximum max-latency of the regions formed. Example: Suppose: api_nodes = 3 m = 3 k = 2 api_from = [1, 2, 3] api_to = [2, 3, 1] api_weight = [4, 5, 3] Function Description: Complete the function optimizeAPIRegions in the editor below. optimizeAPIRegions has the following parameter(s): - api_nodes: the number of cloud regions - api_from[api_edges]: one endpoint of the API connections - api_to[api_edges]: the other endpoint of the API connections - api_weight[api_edges]: the latency of the API connections - k: the maximum number of regions after disabling some APIs Returns: - int: the minimum possible value of the maximum of max-latency of the networks formed Constraints: - 1 ≤ api_nodes ≤ 10^5 - 0 ≤ api_edges ≤ 1.5 × 10^5 - 1 ≤ api_from[i], api_to[i] ≤ api_nodes - 1 ≤ api_weight[i] ≤ 10^9 - 1 ≤ k ≤ n - It is guaranteed that the graph is initially connected. Input Format For Custom Testing: Sample Case 0: Sample Input For Custom Testing: STDIN: api_nodes = 2, api_edges = 1 api_from = [1] api_to = [2] api_weight = [3] k = 1 Sample Output: 3 Explanation: In this case, the graph has 2 nodes connected by 1 API edge with latency 3. Since we are allowed to create only 1 region (k=1), the minimum possible value of the maximum latency of the network is 3.

Asked in: SALESFORCE

Image of the Question

Question Image Question Image Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


from collections import deque
def optimizeAPIRegions(api_nodes, api_from, api_to, api_weight, k):
    graph = [[] for _ in range(api_nodes + 1)]
    all_weights = []
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To solve this problem, it’s essential to understand the structure and requirements: You start with a connected graph representing the Salesforce infrastructure, where nodes are regions and edges are API connections with associated latencies. Your goal is to split this graph into at most k independent connected subgraphs (regions) by disabling some edges (removing connections), in such a way that when the graph is partitioned, the maximum latency of any single edge inside any subgraph is minimized.

Step 1: Understand the Problem Context
- The graph is initially connected, meaning all nodes are reachable via some paths.
- Disabling edges breaks the graph into disconnected components or regions.
- After partitioning, each subgraph’s “max-latency” is defined by the highest latency edge present in that subgraph.
- The goal is to minimize the maximum max-latency across all these subgraphs while ensuring that the total number of subgraphs does not exceed k.

Step 2: Observing the Problem Type
This problem is essentially about partitioning a graph into multiple connected components by removing edges. The critical insight is that you want to remove edges with high latency to split the graph into multiple regions. Doing so can reduce the maximum latency in any individual region by isolating heavy edges.

Step 3: Consider the Nature of Edges and Latencies
The edges with the highest latency are the primary candidates for removal since they contribute the most to the max-latency of any subgraph that contains them. If you remove some of these high-latency edges, the graph breaks into smaller regions, potentially lowering the maximum latency in each.

Step 4: Link to Known Graph Concepts - Minimum Spanning Tree (MST)
The problem can be transformed by considering the Minimum Spanning Tree of the graph, which is a subgraph that connects all nodes with the minimum total latency. The MST contains the “lightest” edges connecting all nodes, and by definition, it minimizes the maximum edge weight necessary to keep the graph connected.

- Construct the MST of the given graph.
- The MST provides a backbone where every node is connected with minimal latency edges.

Step 5: Using MST to Partition the Graph
If you want to partition the graph into at most k regions, this is equivalent to removing edges from the MST to form k connected components.

- Removing an edge in the MST increases the number of connected components by 1.
- To get k components, you need to remove (k-1) edges from the MST.

Step 6: Choosing Which Edges to Remove
To minimize the maximum latency in any resulting region, you should remove the edges with the highest latency in the MST.

- Sort the edges of the MST in descending order of latency.
- Remove the top (k-1) edges with the highest latency to get k connected components.

Step 7: Resulting Maximum Latency
After removing these (k-1) edges, the maximum latency of any region is the largest latency among the remaining edges in the MST (i.e., the maximum latency edge in any component).

Step 8: Implementation Details and Considerations
- First, build the MST using algorithms like Kruskal’s or Prim’s. Kruskal’s is often suitable here due to its edge-based approach and the need to sort edges.
- Keep track of all edges in the MST and sort them by weight.
- Remove the (k-1) heaviest edges to form the required number of components.
- The maximum latency after removal is the latency of the next heaviest edge remaining in the MST.
- If k=1, no edges are removed, and the maximum latency is the highest latency in the MST.

Step 9: Complexity and Constraints
- Since the graph can be large (up to 10^5 nodes and 1.5 × 10^5 edges), the MST construction needs to be efficient.
- Kruskal’s algorithm, combined with a Union-Find (Disjoint Set Union) data structure, can efficiently build the MST in O(E log E) time, which is acceptable here.
- Sorting the MST edges for removal is also efficient because MST edges are at most n-1.

Step 10: Summary
- The problem is transformed into MST construction and edge removal.
- The MST guarantees the minimal maximum latency for the whole network.
- By removing the highest latency edges from the MST, the graph is split into k components, minimizing the maximum latency inside each.
- The final answer is the latency of the heaviest edge remaining in the MST after removing the (k-1) largest edges.

This approach leverages well-known graph theory algorithms and data structures to efficiently solve a problem that might otherwise require complex and inefficient enumeration or search. It elegantly reduces the problem of splitting into k regions with minimal maximum latency to a manageable MST partition problem.
```


Related Questions