Process Restricted Friend Requests - Problem

Imagine you're managing a social network where people can send friend requests, but some pairs of users are forbidden from being connected - either directly or indirectly through mutual friends!

You have n people labeled from 0 to n-1, initially with no friendships. You're given:

  • Restrictions: A list of pairs [xi, yi] who cannot be friends (directly or through a chain of mutual friends)
  • Friend Requests: A sequence of requests [uj, vj] processed in order

Your goal is to determine which friend requests can be approved without violating any restrictions. Each successful request creates a permanent friendship that affects all future requests.

Key Challenge: When two people become friends, their entire friend groups merge! You must ensure that no restricted pairs end up in the same connected group.

Input & Output

example_1.py โ€” Basic restrictions
$ Input: n = 3, restrictions = [[0,1]], requests = [[0,2],[2,1],[0,1]]
โ€บ Output: [true,false,false]
๐Ÿ’ก Note: Request [0,2]: No restriction between 0 and 2, approve. Request [2,1]: Would connect groups {0,2} and {1}, but (0,1) restricted, reject. Request [0,1]: Direct restriction (0,1), reject.
example_2.py โ€” Indirect restrictions
$ Input: n = 5, restrictions = [[0,1],[1,2],[2,3]], requests = [[0,4],[1,2],[3,1],[3,4]]
โ€บ Output: [true,false,false,false]
๐Ÿ’ก Note: Multiple restrictions create complex constraint network. Request [0,4] is safe. [1,2] directly restricted. [3,1] restricted. [3,4] would indirectly connect restricted pairs through 0.
example_3.py โ€” No restrictions violated
$ Input: n = 4, restrictions = [[0,2]], requests = [[1,3],[0,1],[2,3]]
โ€บ Output: [true,true,false]
๐Ÿ’ก Note: First two requests create groups {0,1} and {2,3}. The third request [2,3] would merge these groups, putting restricted pair (0,2) together, so it's rejected.

Visualization

Tap to expand
๐Ÿ•ต๏ธ Spy Network Communication SystemA0A1A2A3โœ“ Secure Linkโœ“ Secure Link๐Ÿšซ FORBIDDEN: A0 โ†” A2๐Ÿ“ž Request: A1 โ†’ A2๐Ÿ” Security Analysis:Current Groups: {A0, A1} and {A2, A3}Proposed Connection: A1 โ†” A2After Connection: All agents in one group {A0, A1, A2, A3}โš ๏ธ SECURITY BREACH: A0 and A2 would be indirectly connected!๐Ÿ›ก๏ธ DECISION: REJECT REQUEST
Understanding the Visualization
1
Network Setup
Initialize agents as isolated nodes with forbidden communication pairs marked
2
Connection Request
Agent requests secure communication channel with another agent
3
Security Check
Verify that establishing connection won't create forbidden indirect paths
4
Decision
Approve and establish connection, or reject to maintain security
Key Takeaway
๐ŸŽฏ Key Insight: Union-Find efficiently tracks connected components while restriction validation ensures security constraints are never violated, even indirectly.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(m * (r + ฮฑ(n)))

For m requests, each requires checking r restrictions plus Union-Find operations with inverse Ackermann time complexity

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Union-Find parent and rank arrays

n
2n
โšก Linearithmic Space

Constraints

  • 2 โ‰ค n โ‰ค 1000
  • 0 โ‰ค restrictions.length โ‰ค 1000
  • restrictions[i].length == 2
  • 0 โ‰ค xi, yi โ‰ค n - 1
  • xi โ‰  yi
  • 1 โ‰ค requests.length โ‰ค 1000
  • requests[j].length == 2
  • 0 โ‰ค uj, vj โ‰ค n - 1
  • uj โ‰  vj
  • All restrictions and requests contain distinct pairs
Asked in
Google 23 Meta 18 Amazon 15 Microsoft 12
28.4K Views
Medium Frequency
~25 min Avg. Time
842 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen