SALESFORCE Coding Question – Solved

7 Live
3. Cluster Queries In a Salesforce global infrastructure, there are customer support clusters numbered from 1 to clusters, distributed across multiple regions. These clusters are interconnected by n communication links, represented by the array connections, such that if two clusters are connected directly or indirectly, they belong to the same support network. Each cluster has a case resolution system to handle customer cases. If a cluster's resolution system goes offline, it forwards the cases to the active cluster in the same support network with the lowest ID, which then resolves the cases. If no active clusters remain in the network, the case is logged as unresolved. You need to process q queries of two types: 1. A case-assignment query: "1 cluster_id", where a case is assigned to the specified cluster. 2. A system-failure query: "2 cluster_id", where the resolution system of the specified cluster goes offline. For each case-assignment query, output the ID of the cluster that resolves the case. Specifically: - When a case-assignment query is made to cluster sender_cluster_id, find the active cluster in the same support network as sender_cluster_id with the lowest ID and output its ID. - If no active clusters remain in the network, output -1. Example: Suppose: clusters = 3 n = 2 connections = [[1, 2], [2, 3]] q = 5 queries = [[2, 2], [1, 2], [2, 1], [2, 3], [1, 1]] Function Description: Complete the function getAssignedCluster in the editor below. getAssignedCluster has the following parameters: - int clusters: number of clusters - int connections[n][2]: the connections between clusters - int queries[q][2]: the queries Returns: - int: the ID of the cluster that resolves the case, or -1 if no active cluster remains in the network. Constraints: - 1 ≤ clusters ≤ 10^5 - 0 ≤ n ≤ 10^5 - 1 ≤ q ≤ 2 × 10^5 - 1 ≤ connections[i][0], connections[i][1] ≤ clusters - 1 ≤ queries[i][0] ≤ 2 - 1 ≤ queries[i][1] ≤ clusters - There is at least 1 query of type 1. Input Format For Custom Testing: The first line contains an integer, clusters. The next line contains an integer, n, the number of interconnections. The next line contains an integer, 2. Each line i of the subsequent n lines (where 0 ≤ i < n) contains two integers, connections[i][0] and connections[i][1]. The next line contains an integer, q, the number of queries. The next line contains an integer, 2. Each line i of the subsequent q lines (where 0 ≤ i < q) contains two integers, queries[i][0] and queries[i][1].

Asked in: SALESFORCE

Image of the Question

Question Image Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


from collections import defaultdict, deque
import heapq
def getAssignedClusters(clusters, connections, queries):
    h = defaultdict(set)
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To approach this problem, start by understanding the structure and constraints: We have clusters connected by communication links forming support networks (connected components). Each cluster can be active or offline. When a cluster goes offline, it no longer resolves cases itself, and cases directed to it should be resolved by the active cluster with the lowest ID in the same connected support network. If no active cluster remains in that network, cases are unresolved.

Step 1: Model the Problem as a Graph with Connected Components
The clusters and their interconnections form an undirected graph. Clusters connected directly or indirectly belong to the same connected component or support network. The key observation is that queries relate to clusters within their respective components.

Step 2: Identify Connected Components
To efficiently answer queries about clusters in the same support network, first identify connected components using a suitable data structure or algorithm like Disjoint Set Union (DSU) or Union-Find. This will help quickly find which clusters belong together.

- Initialize each cluster as its own parent in DSU.
- Union connected clusters based on the given connections.
- After processing all connections, each cluster’s component can be identified by its root or leader.

Step 3: Track Active Clusters in Each Component
Initially, all clusters are active. The problem requires tracking which clusters go offline. We need to quickly determine the lowest active cluster ID in a component at any moment.

- For each component, maintain a data structure to keep track of currently active clusters.
- Since the lowest ID is needed, a balanced structure supporting min queries is suitable (e.g., balanced BST, heap, or a balanced segment tree).
- However, since cluster IDs are integers, specialized data structures like segment trees, Fenwick trees, or balanced BSTs (e.g., TreeSet in Java) are options. Alternatively, storing the active clusters in a balanced ordered set for each component works.

Step 4: Handling Offline Operations (Query Type 2)
When a cluster goes offline, it must be removed from the active set of its component.

- Find the component root of the cluster.
- Remove the cluster ID from that component’s active set.
- Removing must be efficient, as the number of queries can be large.

Step 5: Handling Case-Assignment Queries (Query Type 1)
When a case is assigned to a cluster, find the lowest active cluster in its component.

- Find the component root of the cluster.
- Query the active set for that component to get the minimum cluster ID.
- If no active clusters remain, return -1.

Step 6: Initialization and Data Structures
- Initialize DSU for clusters and merge connected clusters.
- For each component, create a balanced data structure containing all active clusters initially.
- Initially, all clusters are active and inserted into their component’s active set.

Step 7: Edge Cases and Constraints
- Some clusters may be isolated (no connections), forming single-node components.
- Offline operations can make components empty; then queries should return -1.
- Input constraints (up to 10^5 clusters, 2×10^5 queries) require efficient operations—amortized O(log n) for insertion, deletion, and minimum retrieval.

Step 8: Summary of Query Handling
For each query:
- If it’s a failure query (type 2), remove the cluster from its component’s active set.
- If it’s a case-assignment query (type 1), output the minimum cluster ID from the component’s active set or -1 if empty.

Step 9: Optimizations and Implementation Notes
- DSU find operations can be optimized with path compression.
- Union operations should keep track of which clusters belong to which component.
- Active sets could be stored in arrays or maps keyed by component root.
- Keep track of component sizes to optimize merges (union by size/rank).
- Since cluster IDs are unique and limited, a balanced BST or segment tree with lazy propagation can be used to manage active sets efficiently.

This approach leverages graph connectivity concepts and dynamic set operations to efficiently answer queries about active clusters in connected components, meeting the problem’s constraints and requirements.
```


Related Questions