Process Restricted Friend Requests

Problem

Concept

This problem can easily be modelled to Union Find. We need to make connections and make sure that the current connection we are making does not fall in restricted connections.

It is a classic union find problem with an additional check on when we can make unions which is controlled by restrictions

Algorithm

  1. It creates a disjoint set data structure with n nodes.

  2. It iterates over each request.

  3. It checks if the two users involved in the request are already friends. If they are, then it does nothing.

  4. It checks if the two users are connected by a restriction. If they are, then it cannot approve the friend request.

  5. If the two users are not connected by a restriction, then it approves the friend request and merges the two connected components in the disjoint set data structure.

  6. It returns the result array.

Code

Last updated