The 'Network Connectivity' problem focuses on modeling a social network where individuals can form connections, merging their friend groups. This tests your ability to efficiently track group memberships and sizes after a series of connection events, a common pattern in graph algorithms and data structure optimization.
Imagine you're building a social networking platform where users can connect with each other. Initially, every user is in their own isolated network. When two users connect, their networks merge into a single, larger network. You need to implement two functionalities: first, a connect(user1, user2) function that merges the networks of two users; and second, a get_network_size(user) function that returns the number of users in the network to which a given user belongs.
The brute-force approach would involve, for each connect operation, traversing the entire network to update the network memberships of all users. Think of it like manually re-drawing lines between friends on a whiteboard every time two people become friends. Each get_network_size operation would also require traversing the network. This is inefficient because it involves repeatedly revisiting the same connections.
Work through this problem with AI coaching and get real-time feedback
Practice This Problem